2017-03-22 72 views
0

我試圖在模型管理search_fields中包含一個內聯字段,但我無法確定它。Django管理員搜索應該也包含內嵌字段

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

您需要創建您的自定義濾鏡對象,並把它傳遞給管理類的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,)