提高管理员中Django ForeignKey字段的性能 [英] Improving Performance of Django ForeignKey Fields in Admin

查看:179
本文介绍了提高管理员中Django ForeignKey字段的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Django的管理员将admin中的ForeignKey字段作为选择字段,将外部表中的每个记录列为选项。在一个管理员可访问的模型中,我将User模型引用为ForeignKey,由于我有数千个用户,Django正在填充数千个选项。这导致管理页面加载速度非常慢,而且选择不是非常有用,因为可能需要一段时间才能滚动成千上万的选项来查找所需的选项。

By default, Django's admin renders ForeignKey fields in admin as a select field, listing every record in the foreign table as an option. In one admin-accessible model, I'm referencing the User model as a ForeignKey, and since I have thousands of users Django is populating the select with thousands of options. This is causing the admin page to load incredibly slowly, and the select is not very useful since it can take a while to scroll through thousands of options to find the one you want.

为了提高页面加载和可用性,更改此字段的渲染的最佳方法是什么?我想要选择字段被替换为某种按钮来启动搜索表单弹出窗口,或者通过Ajax搜索关键字的文本字段来查找他们想要关联的特定用户的Id。管理员有什么像这样的内置的,还是要从头开始写这个?

What's the best way to change the rendering of this field in order to improve page load and usability? I'd like the select field to be replaced with some sort of button to launch a search form popup, or a text field that searches keywords via Ajax to find the Id for the specific User they want to associate. Does admin have anything like this builtin, or would I have to write this from scratch?

推荐答案

你可以使用几个Django的自动完成应用请查看 Django软件包

You can use one of the few autocomplete apps for Django. Check them at Django Packages.

还有 django-extensions 具有 ForeignKeyAutocompleteAdmin 满足您的需求。

There's also django-extensions that have ForeignKeyAutocompleteAdmin that fit your needs pretty well.

这篇关于提高管理员中Django ForeignKey字段的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆