從1.4版3月23日發佈,2012你可以使用官方django.contrib.admin.SimpleListFilter
這裏是用來只有列表中的活動公司的過濾器代碼示例:
class ActiveCompaniesFilter(SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('active companies')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'project__company'
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
lookup_list = Company.objects.active().values_list('id', 'name').distinct()
# 'objects.active' is a custom Company manager and is
# equivalent to filter 'objects.filter(status=100)'
return lookup_list
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(project__company=self.value())
後打大量的路障我想通了,我用錯了軌道...掌握Django的人應該 1)先學python 2)然後學習django 3)然後使用自己的HTML 4)然後再學無界形式 5進行手動形式),那麼有界形式 6)然後多種形式 7),然後去Django的管理 Django的自動管理是偉大的工具,但如果你會感到興奮,並直接跳上它,你將無法自定義它,你會像我一樣卡住。 現在我跟着上面提到的曲目,感覺好多了。 –
我補充說,很多人選擇Django作爲其'管理'功能......然後意識到他們不能使用它! – Don
鏈接不起作用。請僅發佈相關代碼。 –