如何修改 Django 管理页面中外键字段的呈现方式以避免浏览器崩溃?
我有一个客户模型,其中包含联系人模型的外键。
我的数据库中有超过 100,000 个联系人,当我加载特定客户的管理页面时,该联系人的下拉菜单将填充数据库中的所有联系人。最近,由于其剪切长度,这开始导致我的 Firefox 在加载管理页面时崩溃。
有没有办法:
- 用整数替换字段 我可以手动修改字段 必要时
- 用联系人 ID 替换下拉菜单 替代输入法不会 使浏览器崩溃
- 删除此输入 从客户管理页面 完全
感谢!
I have a Customer model which contains a ForeignKey to a Contact model.
I have over 100,000 contacts in my DB and when I load the admin page for a specific customer, the dropdown menu for the contact is getting populated with ALL of the contacts in the database. This has recently, due to its shear length, started causing my Firefox to crash while the admin page is loading.
Is there a way to either:
- replace the field with an integer
field I can manually modify to the
contact ID when necessary - replace the dropdown menu with some
alternative input method which won't
crash the browser - remove this input
from the Customer admin page
altogether
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做任何您想做的任何事情。
最简单的解决方案是从管理中排除该字段。只是这么说在管理班。
您可以将该字段更改为文本输入并显示其主键而不是项目本身,方法是将其包含在管理类的
raw_id_fields
中。您还可以使用自动完成文本字段输入替换标准下拉小部件。使用实现的小部件或其他等效项。 - 这可能是您最喜欢的解决方案。
您还可以重写
Admin
模型上的formfield_for_foreignkey
方法来自定义显示在外键下拉列表中的查询集。您可能需要查看我的实现仅显示当前用户(或子域)添加的实体。You can do any of the either of things you want to.
Simplest solution is the exclude the field from the admin. Just say so in the admin class.
You can change the field to be text input and display it's primary key rather than the item itself, by including it in the
raw_id_fields
of the admin class.You can also replace the standard dropdown widget with the Auto complete text field input. Use the implemented widget, or other equivalents. - This is probably the solution you like the best.
You can also override the
formfield_for_foreignkey
method on theAdmin
model to customize the queryset that gets displayed in the foreign-key dropdown. You may want to checkout my implementation for displaying only the current User's (or subdomain's) added entities.听起来好像在 admin.py 条目中为相关模型指定
raw_id_fields
中的contact
字段就能解决问题。文档位于此处。附言。令人惊讶(但并不那么惊讶)FF 在你的数据库服务器崩溃之前就给出了......
Sounds like specifying the
contact
field inraw_id_fields
in your admin.py entry for the relevant model would sort you out. Docs are here.PS. Surprised (but not that surprised) that FF gives out before your database server tanks...