silentsokolov / django-admin-rangefilter

A Django app that lets you filter data by date range and numeric range in the admin UI
MIT License
721 stars 106 forks source link

Inability to set a title #92

Closed AntoninGrele closed 1 year ago

AntoninGrele commented 2 years ago

Hello,

By default, the ListFilter class from which DateRangeFilter inherits has a title attribute of None, which can be overriden by setting a different title in the inheriting class.

Unfortunately, I can't use this feature when inheriting from DateRangeFilter because of this :

    def __init__(self, field, request, params, model, model_admin, field_path):
       # ...
        custom_title = self._get_custom_title(request, model_admin, field_path)
        if custom_title:
            self.title = custom_title

If the _get_custom_title method returns something, then the self.title is overriden by that result.

But the _get_custom_title method always returns a result, as we can see here:

    def _get_custom_title(request, model_admin, field_path):
        title_method_name = "get_rangefilter_{0}_title".format(field_path)
        title_method = getattr(model_admin, title_method_name, None)

        if not callable(title_method):
            return None

        return title_method(request, field_path)

So DateRangeFilter will always override the title. The _get_custom_title function should return None if there's no custom titel method, no ?

silentsokolov commented 2 years ago

Hmmm .. I miss something.

Code below checks variable title_method is callable (aka function). getattr checks class has title_method_name. By default class hasn't method with title_method_name -> If title_method is not callable or None method _get_custom_title return None

title_method = getattr(model_admin, title_method_name, None)

if not callable(title_method):
    return None