Override save_model on Django InlineModelAdmin

后端 未结 6 1596
星月不相逢
星月不相逢 2020-12-31 03:38

I have a model that has a user field that needs to be auto-populated from the currently logged in user. I can get it working as specified here if the user

6条回答
  •  离开以前
    2020-12-31 04:15

    Have you tried implementing custom validation in the admin as it is described in the documentation? Overriding the clean_user() function on the model form might do the trick for you.

    Another, more involved option comes to mind. You could override the admin template that renders the change form. Overriding the change form would allow you to build a custom template tag that passes the logged in user to a ModelForm. You could then write a custom init function on the model form that sets the User automatically. This answer provides a good example on how to do that, as does the link on b-list you reference in the question.

提交回复
热议问题