Django admin - sorting list_filter -
i have 'version' in list_filter, , want latest version after 'all' list item. in case, how sort list descending? (all, 3.6.99.108,3.6.99.107...)
define default ordering in model meta
class:
class mymodel(models.model): # model fields here class meta: ordering = ['-version',]
if somehow not fulfill purpose can write own custom list filter:
from django.utils.translation import ugettext_lazy _ django.contrib.admin import simplelistfilter class versionfilter(simplelistfilter): title = _('version') parameter_name = 'version' def lookups(self, request, model_admin): qs = model_admin.queryset(request) return [(i, i) in qs.values_list('version', flat=true) \ .distinct().order_by('-version')] def queryset(self, request, queryset): if self.value(): return queryset.filter(version__exact=self.value()) class mymodel(modeladmin): list_filter = (versionfilter,)
if following error attributeerror: object has no attribute 'queryset'
using django 1.5+
use .get_queryset()
instead e.g.
qs = model_admin.get_queryset(request)
Comments
Post a Comment