Created
January 15, 2023 17:08
-
-
Save sveetch/affcf241ebdf7573c5ba2a4d5b54fc44 to your computer and use it in GitHub Desktop.
Sample admin filter hack to customize choices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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