Django Admin:将外键的选择框更改为搜索自动完成功能,例如搜索对象 [英] Django Admin: change select box for foreign key to search autocomplete, like search objects
本文介绍了Django Admin:将外键的选择框更改为搜索自动完成功能,例如搜索对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Django管理面板在模型的对象列表上具有自动搜索功能,并带有search_fields
,但是现在我有3000个用户.手动添加用户很难通过选择框进行;我需要像搜索字段一样的行为来选择用户foreinkey User
.
The Django admin panel has a search autocomplete with search_fields
on the list of objects of a model, but right now I have 3000 users. To add a user manually is dificult with the selectbox; I need the same behavior like the searchfields for selecting a user foreinkey User
.
如何在表单中包括Django搜索功能,以便在管理面板中进行编辑?
How can I include the Django search feature on the form for editing inside the admin panel?
from myapp.models import Red
from django.contrib.auth.models import User
class Red(models.Model):
customer = models.ForeignKey(User, verbose_name="Cliente")
pub_date = models.DateTimeField(default=datetime.now, blank=True)
推荐答案
Since Django 2.0, you can use autocomplete_fields
to generate autocomplete fields for foreign keys.
class UserAdmin(admin.ModelAdmin):
search_fields = ['username', 'email']
class RedAdmin(admin.ModelAdmin):
autocomplete_fields = ['customer']
这篇关于Django Admin:将外键的选择框更改为搜索自动完成功能,例如搜索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文