Custom querysets for autocomplete fields

805 views
Skip to first unread message

Mattias Linnap

unread,
Aug 4, 2014, 2:01:06 AM8/4/14
to your...@googlegroups.com
Hi all,

I've tested django-autocomplete-light in a recent project, and it works great. But I'm having trouble with figuring out a good way to implement custom querysets instead of objects.all() for my forms.
For example, I have events and users invited to events in my project. The "invite user" form should limit choices to users who are not yet attending that event. Other forms might have other, perhaps more complex queryset restrictions.

http://django-autocomplete-light.readthedocs.org/en/v2/autocomplete.html#overriding-the-queryset-of-a-model-autocomplete-to-secure-an-autocomplete shows how to have a custom queryset for an autocomplete - but it does not help with the use case where the queryset should be based on the context where the field is rendered (event_id).

I've looked at the code a bit, and I guess it could be achieved by overriding a number of parts:
* Register a separate Autocomplete object for each variation of queryset restrictions,
* In AutocompleteModelBase.choices_for_request(), obtain the context from request.GET (for example event_id to exclude users),
* In AutocompleteInterface.get_absolute_url(), somehow get the event_id from the form/view context, and add it to the URL (haven't figured out how to do this yet).

But even if I figure out the last part, it seems a bit tedious and not at all DRY to have to add this much boilerplate for each custom queryset, when the standard django.forms.ModelChoiceField has a nice queryset argument that can be set in the form __init__ method.

Do you have any suggestions on the best approach to building autocompletes whose queryset depends on the parameters of the view that is rendering the form?

Thanks,

Mattias

James Pic

unread,
Aug 4, 2014, 2:50:01 AM8/4/14
to your...@googlegroups.com
Hi Mattias, did you read about [dependencies between
autocompletes](http://django-autocomplete-light.readthedocs.org/en/v2/dependant.html)
?

Mattias Linnap

unread,
Aug 5, 2014, 6:51:56 AM8/5/14
to your...@googlegroups.com
Thanks for the link, I had not read that yet. Adding the URL components in Javascript should be much easier than overriding AutocompleteInterface.get_absolute_url().

I'll investigate another approach that might be more widely applicable as well: sending the whole queryset filter to the autocomplete, instead of creating a separate autocomplete for every query filter. It seems that this might be possible by pickling the .query property of a queryset, which can then be used to re-create and re-query it later: https://docs.djangoproject.com/en/dev/ref/models/querysets/#pickling-querysets
It should be secure against user tampering as long as the serialised format is signed with https://docs.djangoproject.com/en/dev/topics/signing/#using-the-low-level-api

Does that seem like a reasonable thing to do?
Reply all
Reply to author
Forward
0 new messages