如何将 ModelChoiceField 转换为管理中的仅显示字段
现在,我有一个带有容器和页面 stackedInline 的管理页面。这些页面中有 2 个外键,随着这些外键值的数量增加,页面需要永远呈现。
我如何设法显示外键的值,但不让它在管理中成为可更改的东西?我确实想在内联中显示 str(ForeignModel),但我不希望为每个页面模型加载一次包含 1000 个选项的选择框。
我记得在某处读过这个内容,但似乎无法再找到它了。如果它不在管理员中,我可以更改小部件,但我不知道如何在管理员中执行此操作。
Right now, I have an admin page with a Container and a stackedInline for the Pages. The pages have 2 ForeignKeys in them and as the # of values for those ForeignKeys grows, the page takes forever to render.
How do I manage to display the value of the ForeignKey, but NOT let it be a changeable thing in the admin? I definetely want to display the str(ForeignModel) in the inlines, but I don't want a select box with several 1000 choices to be loaded once for each Page model.
I remember reading about this somewhere, but can't seem to locate it anymore. If it wasn't in the admin, I could just change out the widget, but I don't know how to do that in the admin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是在谈论 ModelAdmin.readonly_fields 吗?
而且,为了将来的参考,可以很容易地在管理中更改小部件。
对于特定字段:
对于特定类型的所有字段:
Are you talking about ModelAdmin.readonly_fields?
And, for future reference, it's easy to change out the widget in the admin.
For a specific field:
For all fields of a particular type: