openwisp-controller icon indicating copy to clipboard operation
openwisp-controller copied to clipboard

[change] Optimize queries of sortedm2m

Open nemesifier opened this issue 5 years ago • 1 comments

The sortedm2m widget generates too many unnecessary queries, we should find a way to avoid this: https://github.com/jazzband/django-sortedm2m/issues/115

nemesifier avatar Jun 09 '20 23:06 nemesifier

I have thought about a possible solution for this.

By default, all the templates accessible to the user are included in the response. On systems with many templates, this slows down the page a lot.

Now that we'll be loading the templates with JS only (#428), we could take this one step further.

We could edit the queryset of the sortedm2m admin field to not include any template:

def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == 'templates':
            kwargs['queryset'] = Template.objects.none()
        return super().formfield_for_manytomany(db_field, request, **kwargs)

Then we'd have to edit the JS so that it handles the creation of HTML checkboxes (instead of showing what's already there).

Before implementing this fully, we should test if trying to save any option which is not present in the admin queryset works at all. If it does, we should be able to do this.

nemesifier avatar Apr 13 '21 02:04 nemesifier