Django admin:将选定的相关字段框更改为自动完成 [英] Django admin: Change selected box of related fields to autocomplete

查看:25
本文介绍了Django admin:将选定的相关字段框更改为自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些模型将用户作为外键.但是我们的系统中有大约 25000 名用户,要找到我们需要的用户有点令人生畏.

We have some models that are have a user as a foreign key. But with about 25000 users in our system, it's a bit daunting to find the one we need.

有没有比选择框更好的解决方案?也许是自动完成,所以我们可以开始输入用户名/地址?还是只是一个搜索框?当为这些对象切换相关用户时,25000 个未排序的用户变得越来越难.

Is there a solution out there that's better than the select box? Maybe an autocomplete so we can start typing the user name / address? Or just a search box? When switching the related user for these objects, it's getting harder and harder with 25000 unsorted users.

即使只是将其设置为按用户名对用户进行排序也会有所帮助.

Even just setting it to sort the users by username would be helpful.

推荐答案

我遇到了这个问题,我的结论是改用自动完成字段.在大多数情况下它工作得很好.唯一的问题是当您有很多几乎相同的条目时.例如,在您的情况下,如果您键入 Robert 作为名称,并且列表中有几百个 Robert 条目...

I had this problem and my conclusion was to use an autocomplete field instead. It works pretty well in most cases. The only problem is when you have a lot of entries that are mostly the same. For example, in your case, if you type Robert for the name and there's a few hundred Robert entries in the list...

更新

正如 shuckc 的回答中提到的,Django 2.0+ 管理员现在 内置自动完成.

As mentions in shuckc's answer, Django 2.0+ admin as now autocomplete built in.

对于较旧的 Django 或在管理员之外使用(旧答案)

有许多应用程序为 Django 管理员添加了自动完成功能:

There are many apps that add autocomplete to the Django admin:

  • django-autocomplete-light
  • django-extensions (ForeignKeyAutocompleteAdmin)
  • django-autocomplete (on google code)
  • django-ajax-selects
  • django-admin-autocomplete
  • django-autocomplete (tyrion)

我最喜欢的是最后一个.写的很好,可以在admin和admin之外使用,可以和ManyToManyFields、ForeignKeyFields、CharFields等一起使用.

My preferred one is the last one. It's well written, it can be used with the admin and outside of the admin, it works with ManyToManyFields, ForeignKeyFields, CharFields, etc.

我为我的客户做了一个 fork,添加了一些细节,比如ForeignKeyRawIdWidget 之类的查找(放大镜)按钮.

I did a fork of this project for my client that adds some niceties like a lookup (loupe) button like the ForeignKeyRawIdWidget.

这篇关于Django admin:将选定的相关字段框更改为自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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