我正在尝试在模型管理员search_fields中包含一个内联字段,但我无法理解它。
from django.contrib import admin
from yourapp.models import Supplier, Product
class ProductInline(admin.TabularInline):
model = Product
class SupplierAdmin(admin.ModelAdmin):
inlines = [ProductInline,]
admin.site.register(Supplier, SupplierAdmin)
在这里,我想在SupplierAdmin类中搜索产品,虽然产品是内联的,但我无法获得搜索功能
答案 0 :(得分:0)
您需要创建自定义Filter对象并将其传递给Admin类的list_filters属性。有关详细信息,请参阅documentation
基本上你需要的是一个像:
这样的过滤器class ProductFilter(admin.SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = 'Filter title'
# Parameter for the filter that will be used in the URL query.
parameter_name = 'name_of_parameter'
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
# Compare the requested value (either '80s' or 'other')
# to decide how to filter the queryset.
if self.value():
return queryset.filter(product_FIELD_YOU_WANT_TO_FILTER=self.value())
else:
return queryset
class SupplierAdmin(admin.ModelAdmin):
inlines = [ProductInline,]
list_filter = (ProductFilter,)