By extending SimpleListFilter
, you can add custom filter to list_filter
.
from django.contrib.admin import SimpleListFilter
class CountryFilter(SimpleListFilter):
title = 'country' # or use _('country') for translated title
parameter_name = 'country'
def lookups(self, request, model_admin):
countries = set([c.country for c in model_admin.model.objects.all()])
return [(c.id, c.name) for c in countries] + [
('AFRICA', 'AFRICA - ALL')]
def queryset(self, request, queryset):
if self.value() == 'AFRICA':
return queryset.filter(country__continent='Africa')
if self.value():
return queryset.filter(country__id__exact=self.value())
class CityAdmin(ModelAdmin):
list_filter = (CountryFilter,)
'backend > django' 카테고리의 다른 글
Django: add extra context to admin changelist view (0) | 2018.05.10 |
---|---|
Django: multiple choices in django admin list_filter (0) | 2018.05.09 |
Django: check changed fields in Model save() method (0) | 2018.05.04 |
Django: ModelChoiceField - prepopulate choice field with row from model (0) | 2018.05.03 |
Django: disable password check (password2) in django-allauth (0) | 2018.05.02 |