Django管理搜索应该包括内联字段

2024-10-04 11:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在模型管理搜索_字段中包含一个内联字段,但我无法计算它。在

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类中搜索产品,虽然产品是内联的,但我无法获得搜索功能


Tags: djangofrom模型importadmin产品modelsproduct
1条回答
网友
1楼 · 发布于 2024-10-04 11:29:16

您需要创建自定义过滤器对象并将其传递给Admin类的list\u 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,)

相关问题 更多 >