产生背景
当 ForeignKey 数据过多,手动在后台添加时不方便找到自己的想要数据,遂想要一个可以搜索检索数据功能。
检索文档
https://stackoverflow.com/questions/30214469/in-django-admin-how-to-add-filter-or-search-for-foreign-key-select-box/50356922
With this you can use the autocomplete_fields
on a foreignkey
field on your source admin class and as before set the search_fields
on the target admin class.
配置 admin
代码语言:javascript复制class AnchorBindAgentAdmin(admin.ModelAdmin):
list_display = ["agent", "anchor", "proportion", "start_time", "end_time", "create_time"]
list_display_links = ["agent", "anchor", "proportion", "start_time", "end_time", "create_time"]
list_filter = ["agent", "anchor", "start_time", "end_time"]
list_per_page = 20 # 每页数量
readonly_fields = ["start_time"]
autocomplete_fields = ["anchor"] # 带有搜索框的外键选择框
配置后效果
注意事项
被搜索的字段需要在其对应对的模型的 admin 中加入到 search_field