在 ModelAdmin 与 ModelForm 中排除
ModelForm 中的排除列表与 ModelAdmin 中的排除列表有什么不同吗?如果我的 ModelForm 与 ModelAdmin 绑定,我需要在哪里指定理想的排除列表?在 ModelForm Meta 类中还是在 ModelAdmin 子类中?
Is the exclude list in a ModelForm any different from an exclude list in a ModelAdmin? If my ModelForm is tied to the ModelAdmin, where do I need to specify the exclude list ideally; in the ModelForm Meta class or in the ModelAdmin subclass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
取决于您是否想对表单执行其他操作。如果它显示在管理员之外的其他位置,并且您也想排除其中的相同字段,请在 ModelForm 中定义它。如果 ModelForm 仅在管理中使用,而没有在其他地方使用,那么您基本上可以选择对您更有意义的方式。我个人仍然会将其保留在 ModelForm 中,因此此功能与它相关,而不是与管理员相关。
编辑(参见下面的评论):
显然 Django 中似乎存在一个错误。如果我排除 ModelForm 中的某些内容,然后在 ModelAdmin 中使用此表单,由于某种原因它仍然显示此字段。您最好在管理中排除以确保 100% 确定,或者在 ModelForm 中指定
fields
而无需指定要排除的字段。Depends if you want to do something else with the form. If its displayed somewhere else than the admin and you want to exclude the same field there as well, define it in the ModelForm. If the ModelForm is just used in the admin and nowhere else you basically can choose what makes more sense for you. I personally would still keep it in the ModelForm so this functionality is tied to it instead to the admin.
Edit (see comments below):
Apparently there seems to be a bug in Django. If I exclude something in a ModelForm and then use this form in the ModelAdmin it is still showing this field for some reason. You better exclude in the admin only to be 100% sure or specify
fields
in the ModelForm without the field you want to exclude.