django admin에 대한 일반 필터 인 'is_staff'및 'is_superuser'대신 사용자 정의 필터를 만들고 싶습니다. 나는이 list_filter을 장고 문서에서 읽었다. 이 같은 list_display에 대한 사용자 정의 기능을장고 관리의 list_filter에 대한 사용자 정의 필터 만들기
from datetime import date
from django.utils.translation import ugettext_lazy as _
from django.contrib.admin import SimpleListFilter
class DecadeBornListFilter(SimpleListFilter):
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('decade born')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'decade'
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.
"""
return (
('80s', _('in the eighties')),
('90s', _('in the nineties')),
)
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 '90s')
# to decide how to filter the queryset.
if self.value() == '80s':
return queryset.filter(birthday__gte=date(1980, 1, 1),
birthday__lte=date(1989, 12, 31))
if self.value() == '90s':
return queryset.filter(birthday__gte=date(1990, 1, 1),
birthday__lte=date(1999, 12, 31))
class PersonAdmin(ModelAdmin):
list_filter = (DecadeBornListFilter,)
하지만 난 이미 만든 : 사용자 정의 필터를이 방식으로 작동
def Student_Country(self, obj):
return '%s' % obj.country
Student_Country.short_description = 'Student-Country'
이 가능 내가 쓰는 대신 list_filter에 list_display에 대한 사용자 정의 기능을 사용할 수 있나요 list_filter에 대한 새로운 사용자 정의 함수? 모든 제안이나 개선을 환영합니다 .. 이것에 대한 지침이 필요합니다 ... 감사합니다 ...
에 http : // 유래. com/questions/12215751/can-i-make-list-filter-in-django-admin-to-only-show-referenced-foreignkeys – user956424