[Answered ]-Custom BooleanField name in Django admin list filter

1👍

You can replicate its behavior with:

from django.contrib import admin
from django.utils.translation import gettext_lazy as _

NEW_BUILDING_MAPPING = {'true': True, 'false': False, 'none': None}


class NewBuildingListFilter(admin.SimpleListFilter):
    title = _('New building')
    parameter_name = 'is_new_building'

    def lookups(self, request, model_admin):
        return (
            ('true', _('New')),
            ('false', _('Old')),
            ('none', _('Unknown')),
        )

    def queryset(self, request, queryset):
        val = self.value()
        if val is not None:
            return queryset.filter(is_new_building=NEW_BUILDING_MAPPING[val])
        return queryset

and then plug in this filter with:

class FlatAdmin(admin.ModelAdmin):
    list_filter = (NewBuildingListFilter,)

Leave a comment