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

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

  • HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
  • django admin 列表禁用删除操作,编辑页面禁用删除按钮
  • django assignment_tag 赋值标签
  • django 获取 request 的 url
  •