How do I create a filter in Django Admin to only display records where an integer value lies between two values? For example, if I have a model Person, which has an age attribute, and I only want to display Person records where age is between 45 and 65.
You can Filter the field some what like this by using queryset()
Function ... I had used SimpleListFilter
def queryset(self, request, queryset):
filt_age = request.GET.get('parameter_name')
return queryset.filter(
age__range=self.age_dict[filt_age]
)
And create dict in lookups()
and return it According to the age
def lookups(self, request, model_admin):
return [
(1, '5-21'),
(2, '22-35'),
(3, '35-60')
]
What you are looking is http://djangosnippets.org/snippets/587/ - the snippet is kinda old but works just fine after an additional minor change.
I uploaded the patched version at https://gist.github.com/1009903
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With