在 ModelAdmin 与 ModelForm 中排除

发布于 2024-11-05 21:25:30 字数 134 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-11-12 21:25:30

取决于您是否想对表单执行其他操作。如果它显示在管理员之外的其他位置,并且您也想排除其中的相同字段,请在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文