Skip to content

Instantly share code, notes, and snippets.

@sveetch
Created January 15, 2023 17:08
Show Gist options
  • Save sveetch/affcf241ebdf7573c5ba2a4d5b54fc44 to your computer and use it in GitHub Desktop.
Save sveetch/affcf241ebdf7573c5ba2a4d5b54fc44 to your computer and use it in GitHub Desktop.
Sample admin filter hack to customize choices
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from ..choices import get_language_choices, get_language_default
class DeprecatedLanguageListFilter(admin.SimpleListFilter):
"""
Add Human-readable language title as defined in LANGUAGES setting.
The behavior of this filter have been largely tested and was feeling awful, default
listing on default language is not very obvious.
It is keeped for now as it implement a way to hack the choices.
"""
title = _("language")
# Parameter for the filter that will be used in the URL query.
parameter_name = "lang"
def choices(self, changelist):
"""
Hack the filter list to change the default name to match this specific filter
behavior.
"""
choices = list(super().choices(changelist))
choices[0]["display"] = _("Default language")
return choices
def lookups(self, request, model_admin):
"""
Add again the "All" entry but with a special keyword "all" to use to select the
right queryset.
"""
base_choices = list(get_language_choices())
return [('all', _('All'))] + base_choices
def queryset(self, request, queryset):
"""
Use the default language for default queryset (with no URL argument) or
return article queryset for all language (if ``all`` argument is given) or
return article queryset filtered on given language code (from given argument).
"""
if self.value() and self.value() == "all":
return queryset.filter()
elif self.value():
return queryset.filter(language=self.value())
else:
return queryset.filter(language=get_language_default())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment