Skip to content

Instantly share code, notes, and snippets.

@nonamenix
Created August 12, 2018 07:11
Show Gist options
  • Save nonamenix/1882493d21fc3d214e43bcdde58256e4 to your computer and use it in GitHub Desktop.
Save nonamenix/1882493d21fc3d214e43bcdde58256e4 to your computer and use it in GitHub Desktop.
NullFilter for django admin
from django.contrib.admin import SimpleListFilter
from django.utils.translation import gettext_lazy as _
class NullFilter(SimpleListFilter):
title = ''
parameter_name = ''
def lookups(self, request, model_admin):
return (
('0', _('None'),),
)
def queryset(self, request, queryset):
kwargs = {
'%s' % self.parameter_name: None,
}
if self.value() == '0':
return queryset.filter(**kwargs)
if self.value() == '1':
return queryset.exclude(**kwargs)
return queryset
class PriceNullFilter(NullFilter):
title = _('Price')
parameter_name = 'price'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment