仅当条件匹配时 Django 显示列表过滤器
Django show list filter only if condition matches
我只想在特定条件匹配时为 Django 管理员显示特定列表过滤器。例如,我现在有 3 个过滤器:country
、state
、city
。同时显示所有 3 个会产生一个真正的混乱和一个非常长的侧边栏,因为它结合了一长串城市、州和国家。
我想做的是首先只显示国家,当点击一个国家时我想显示那个国家的州和城市过滤器。这是默认可行的还是我必须自己创建自定义过滤器?
list_filter = (
('loc_country_code', custom_titled_filter( 'country' )),
('loc_state', custom_titled_filter( 'state' )),
('loc_city', custom_titled_filter( 'city' )),
)
您可以创建自定义 SimpleListFilter
以在您的管理员上生成动态过滤器。在 SimpleListFilter
中,如果 lookups
方法 returns 为空 tuple/list,过滤器将被禁用(也从视图中隐藏)。这可用于控制何时出现某些过滤器。
这是一个基本过滤器:
class CountryFilter(admin.SimpleListFilter):
title = 'Country'
parameter_name = 'country'
def lookups(self, request, model_admin):
""" Return a list of (country_id, country_name) tuples """
countries = Country.objects.all()
return [(c.id, c.name) for c in countries]
def queryset(self, request, queryset):
...
下面是一个过滤器,其中基于上面的过滤器限制了选项:
class StateFilter(admin.SimpleListFilter):
title = 'State'
parameter_name = 'state'
def lookups(self, request, model_admin):
"""
Return a list of (state_id, state_name) tuples based on
country selected
"""
# retrieve the current country the user has selected
country_id = request.GET.get('country')
if country_id is None:
# state filter will be hidden
return []
# only return states which belong in the country
states = State.objects.filter(country_id=country_id)
return [(s.id, s.name) for s in states]
def queryset(self, request, queryset):
...
总体思路是在过滤器 类 上使用 lookups
来限制后续过滤器的选项。这些过滤器可以通过 list_filter
参数应用于管理员。
class MyAdmin(admin.ModelAdmin):
list_filter = [CountryFilter, StateFilter, CityFilter, ...]
我只想在特定条件匹配时为 Django 管理员显示特定列表过滤器。例如,我现在有 3 个过滤器:country
、state
、city
。同时显示所有 3 个会产生一个真正的混乱和一个非常长的侧边栏,因为它结合了一长串城市、州和国家。
我想做的是首先只显示国家,当点击一个国家时我想显示那个国家的州和城市过滤器。这是默认可行的还是我必须自己创建自定义过滤器?
list_filter = (
('loc_country_code', custom_titled_filter( 'country' )),
('loc_state', custom_titled_filter( 'state' )),
('loc_city', custom_titled_filter( 'city' )),
)
您可以创建自定义 SimpleListFilter
以在您的管理员上生成动态过滤器。在 SimpleListFilter
中,如果 lookups
方法 returns 为空 tuple/list,过滤器将被禁用(也从视图中隐藏)。这可用于控制何时出现某些过滤器。
这是一个基本过滤器:
class CountryFilter(admin.SimpleListFilter):
title = 'Country'
parameter_name = 'country'
def lookups(self, request, model_admin):
""" Return a list of (country_id, country_name) tuples """
countries = Country.objects.all()
return [(c.id, c.name) for c in countries]
def queryset(self, request, queryset):
...
下面是一个过滤器,其中基于上面的过滤器限制了选项:
class StateFilter(admin.SimpleListFilter):
title = 'State'
parameter_name = 'state'
def lookups(self, request, model_admin):
"""
Return a list of (state_id, state_name) tuples based on
country selected
"""
# retrieve the current country the user has selected
country_id = request.GET.get('country')
if country_id is None:
# state filter will be hidden
return []
# only return states which belong in the country
states = State.objects.filter(country_id=country_id)
return [(s.id, s.name) for s in states]
def queryset(self, request, queryset):
...
总体思路是在过滤器 类 上使用 lookups
来限制后续过滤器的选项。这些过滤器可以通过 list_filter
参数应用于管理员。
class MyAdmin(admin.ModelAdmin):
list_filter = [CountryFilter, StateFilter, CityFilter, ...]