如何修改 Django 管理页面中外键字段的呈现方式以避免浏览器崩溃?

发布于 2024-09-10 08:58:15 字数 272 浏览 3 评论 0原文

我有一个客户模型,其中包含联系人模型的外键。

我的数据库中有超过 100,000 个联系人,当我加载特定客户的管理页面时,该联系人的下拉菜单将填充数据库中的所有联系人。最近,由于其剪切长度,这开始导致我的 Firefox 在加载管理页面时崩溃。

有没有办法:

  1. 用整数替换字段 我可以手动修改字段 必要时
  2. 用联系人 ID 替换下拉菜单 替代输入法不会 使浏览器崩溃
  3. 删除此输入 从客户管理页面 完全

感谢!

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:

  1. replace the field with an integer
    field I can manually modify to the
    contact ID when necessary
  2. replace the dropdown menu with some
    alternative input method which won't
    crash the browser
  3. remove this input
    from the Customer admin page
    altogether

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

2024-09-17 08:58:15

您可以做任何您想做的任何事情。

最简单的解决方案是从管理中排除该字段。只是这么说在管理班。

您可以将该字段更改为文本输入并显示其主键而不是项目本身,方法是将其包含在管理类的 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 the Admin 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.

风渺 2024-09-17 08:58:15

听起来好像在 admin.py 条目中为相关模型指定 raw_id_fields 中的 contact 字段就能解决问题。文档位于此处

附言。令人惊讶(但并不那么惊讶)FF 在你的数据库服务器崩溃之前就给出了......

Sounds like specifying the contact field in raw_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...

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