How can I change the default filter choice from 'ALL'? I have a field named as status
which has three values: activate
, pending
and rejected
. When I use list_filter
in Django admin, the filter is by default set to 'All' but I want to set it to pending by default.
The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.
Run 'python manage.py migrate' to apply them. Username (leave blank to use 'chatru'): admin Email address: [email protected] Password: Password (again): The password is too similar to the username.
In order to achieve this and have a usable 'All' link in your sidebar (ie one that shows all rather than showing pending), you'd need to create a custom list filter, inheriting from django.contrib.admin.filters.SimpleListFilter
and filtering on 'pending' by default. Something along these lines should work:
from datetime import date from django.utils.translation import ugettext_lazy as _ from django.contrib.admin import SimpleListFilter class StatusFilter(SimpleListFilter): title = _('Status') parameter_name = 'status' def lookups(self, request, model_admin): return ( (None, _('Pending')), ('activate', _('Activate')), ('rejected', _('Rejected')), ('all', _('All')), ) def choices(self, cl): for lookup, title in self.lookup_choices: yield { 'selected': self.value() == lookup, 'query_string': cl.get_query_string({ self.parameter_name: lookup, }, []), 'display': title, } def queryset(self, request, queryset): if self.value() in ('activate', 'rejected'): return queryset.filter(status=self.value()) elif self.value() == None: return queryset.filter(status='pending') class Admin(admin.ModelAdmin): list_filter = [StatusFilter]
EDIT: Requires Django 1.4 (thanks Simon)
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