KH's approach to a base class that can be easily reused was very useful to me. I couldn't get the written example to work, but with a little tweaking, it worked fine (Python 2.7, Django 1.10) to achieve this.
from django.contrib import admin class NotNullFilter(admin.SimpleListFilter): title = 'Filter title not set' parameter_name = 'parameter name not set' def lookups(self, request, model_admin): return ( ('not_null', 'Not empty only'), ('null', 'Empty only'), ) def queryset(self, request, queryset): filter_string = self.parameter_name + '__isnull' if self.value() == 'not_null': is_null_false = { filter_string: False } return queryset.filter(**is_null_false) if self.value() == 'null': is_null_true = { filter_string: True } return queryset.filter(**is_null_true) class YoutubeNotNullFilter(NotNullFilter): title = "Youtube" parameter_name = "youtube_videoid" class SomeVideoClass(admin.ModelAdmin): ... list_filter = [YouTubeNotNullFilter, ...]
source share