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

Performance issue with multiple range filters (same resources : scripts + styles loaded multiple times) #116

Open daweedm opened 8 months ago

daweedm commented 8 months ago

The DOM loading of list_filters is slowed down only when one ore more date range filters are used.

I think this is related to the scripts <script src=...> (and styles) that are defined in the following template :

https://github.com/silentsokolov/django-admin-rangefilter/blob/ceb7ed7c750f496f400816760f465f76ad3017cd/rangefilter/templates/rangefilter/date_filter.html#L71-L146

This template is loaded for every "instance" of date filter and make the browser download the same scripts & css multiple times, which slows down the DOM rendering (mostly because of the repetition of scripts inside the HTML document).

A solution for this would be to define a Mixin for the Admin class which defines the needed resources only once using the Media subclass :

class RangeFilterMixin(admin.ModelAdmin):
    class Media:
        css = {
            'all': [
                'path/to/some.css'
            ]
        }

        js = [
            'path/to/some.js'
        ]

I can make a PR, but this will introduce breaking changes, since the inline scripts & resources will be removed from the date_filter.html template and the Mixin will be required to make the filter work.

What do you think about it ? Maybe you have another solution for this ?

silentsokolov commented 8 months ago

A solution for this would be to define a Mixin for the Admin class which defines the needed resources only once using the Media subclass.

I think you could try improve OnceCallMedia

Mixin will be required to make the filter work

I don't think that is a good idea