Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Custom Filters for list_filter in Django Admin

I would like to make custom filters for django admin instead of the normal 'is_staff' and 'is_superuser'. I have read this list_filter in Django docs. Custom Filters work in this way:

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,) 

But i have already made custom functions for list_display like this:

def Student_Country(self, obj):     return '%s' % obj.country Student_Country.short_description = 'Student-Country' 

Is it possible i could use the custom functions for list_display in list_filter instead of writing a new custom function for list_filter? Any suggestions or improvements are welcome.. Need some guidance on this... Thanks...

like image 556
lakshmen Avatar asked Aug 24 '12 03:08

lakshmen


People also ask

What is list filter in Django admin?

Django allows the user of the admin site to filter the instances of a Model by adding the list_filter attribute to your ModelAdmin objects. You can find more information about the Django's filtering utilities in the official documentation, in the Django Admin Site section.

Is Django's admin interface customizable if yes then how?

To implement it in your project, make a new app in your Django project named products. Install this application, type product in the INSTALLED_APPS list in settings.py file. We will now make models in the products app. The model will be used throughout the tutorial to customize the Django Admin.


1 Answers

You can indeed add custom filters to admin filters by extending SimpleListFilter. For instance, if you want to add a continent filter for 'Africa' to the country admin filter used above, you can do the following:

In admin.py:

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,) 
like image 64
Rick Westera Avatar answered Oct 14 '22 23:10

Rick Westera