Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Created May 15, 2013 10:54
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save guillaumepiot/5583149 to your computer and use it in GitHub Desktop.
Save guillaumepiot/5583149 to your computer and use it in GitHub Desktop.
DJANGO - Admin custom list filter
class CategoryListFilter(SimpleListFilter):
# USAGE
# In your admin class, pass trhe filter class as tuple for the list_filter attribute:
#
# list_filter = (CategoryListFilter,)
# Human-readable title which will be displayed in the
# right admin sidebar just above the filter options.
title = _('categories')
# Parameter for the filter that will be used in the URL query.
parameter_name = 'category'
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.
"""
list_tuple = []
for category in Category.objects.get_published_original():
#print category
list_tuple.append((category.id, category.translated().title))
return list_tuple
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(category__id=self.value())
else:
return queryset
@gklka
Copy link

gklka commented Dec 19, 2013

SimpleListFilter import is:

from django.contrib.admin import SimpleListFilter

@ArnoldoRicardo
Copy link

exactly what i'm looking for.

thanks

@E-M-P-I-R-E
Copy link

+1 thank you!

@BlackPie
Copy link

BlackPie commented Nov 3, 2016

Thanks!

@kendalvictor
Copy link

well explained thank you

@kayode-adechinan
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment