97
You will need to have a custom filter class, but you can actually implement a custom filter class factory that you can use everywhere you just need a filter with a custom title:
from django.contrib import admin
def custom_titled_filter(title):
class Wrapper(admin.FieldListFilter):
def __new__(cls, *args, **kwargs):
instance = admin.FieldListFilter.create(*args, **kwargs)
instance.title = title
return instance
return Wrapper
After that in your ModelAdmin
class:
list_filter = (
('fieldname', custom_titled_filter('My Custom Title')),
'plain_field',
...
)
(Note how the custom filter is not just a field name, but a tuple of (field_name, CustomFilterClass)
, you’re just getting your CustomFilterClass
from your custom_titled_filter()
factory)
65
if you define labels on your fields in your model, you should see the change on filter options: Like,
is_staff = models.BooleanField(verbose_name="My Best Staff's", default=False)
Here “My Best Staff’s” is filter Label.
- [Django]-How do I make an auto increment integer field in Django?
- [Django]-How can I filter a Django query with a list of values?
- [Django]-Difference between django-redis-cache and django-redis for redis caching with Django?
Source:stackexchange.com