Adding a title option to ModelAdmin.list_filter

243 views
Skip to first unread message

Philipp DC

unread,
May 3, 2020, 6:35:40 AM5/3/20
to Django developers (Contributions to Django itself)

Hey, I recently took over a ticket regarding adding a title option to list_filter and I would like to hear your opinion on how the API for this should look like.


The idea of this ticket is that you can add the title that is used for the filter directly to list_filter if you don't like the default name. Currently, the only way to change the title is by creating a subclass of FieldListFilter, set the class attribute title of this class and pass it as list_filter=[(field, custom_class)]. Especially if the field is a foreign key the default name (which is the verbose_name) might not make much sense in this context. The pull request currently proposes to add a new way by allowing to add a 2-tuple (field, title) to list_filter like e.g. ('tags__tag', 'Related Tag')


Ticket: https://code.djangoproject.com/ticket/29232

Pull request: https://github.com/django/django/pull/12837


Fran Hrženjak

unread,
May 3, 2020, 9:09:04 AM5/3/20
to django-d...@googlegroups.com
+1 on the proposed changes.
Very little extra complexity for something useful.

Would it be more future-proof if the second param was a dict like {“title”: “Related Tag”}? Maybe, not sure. KISS, YAGNI and all that? I could get behind either choice.

I would love for this to also work:

class MyStaffFilter(BooleanFieldListFilter): 
    title = "By can haz admin"  # no effect :(  overwritten in __init__

@admin.register(User)
class UserAdmin(UserAdminBase):
    list_filter = (
        ("is_staff", MyStaffFilter),
    )

A problem with taking only this approach is that user (developer) has to know exactly which class of filter to subclass, which can be a bit tricky (e.g. RelatedFieldListFilter or RelatedOnlyFieldListFilter?) I’d argue that benefits for easy development outweigh the added complexity of the PR.

/$.02


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/b7be92f8-7aac-47e5-abbf-03ef92a98a28%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages