silentsokolov / django-admin-rangefilter

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

Possible to turn on by default on all admin #96

Closed zehawki closed 1 year ago

zehawki commented 1 year ago

Adding this to each admin field one by one is cumbersome... Is there some way to add by default for fields that are present in all models, eg created_at, modified_at (in my case).

silentsokolov commented 1 year ago

You can override __init__ method of ModelAdmin and add something like this:

def __init__(self, *arg, **kwarg):
    super().__init__(self, *arg, **kwarg)
    fields = ['created_at', 'modified_at']

    def _default_range(self, request)
        return 0

    for f in fields:
        setattr(self.__class__, f'get_rangefilter_{f}_default', _default_range)