I wanted the filter to show only Yes/No (without All), and to have Yes by default. That way I can avoid some of the confusion for the user to see the All option selected while actually having the Yes filter applied.
I came up with this:
class DefaultYesBooleanFieldListFilter(BooleanFieldListFilter):
default = "1"
def __init__(self, field, request, params, model, model_admin, field_path):
super().__init__(field, request, params, model, model_admin, field_path)
if not self.lookup_val:
self.lookup_val = self.default
self.used_parameters[self.lookup_kwarg] = self.default
def choices(self, changelist):
choices = super().choices(changelist)
choices.__next__()
for choice in choices:
print(choice)
yield choice
class DefaultNoBooleanFieldListFilter(DefaultYesBooleanFieldListFilter):
default = "0"
Usage: the same as a normal BooleanFieldListFilter
, that is,
class SomethingAdmin(admin.ModelAdmin):
list_filter = (
("active", DefaultYesBooleanFieldListFilter),
)
Note that users will still have the general link to show all results (or remove all filters) which will still cause some confusion.
I hope it's helpful for someone.