I’ve made this change locally in Django 5.0, to solve this problem, and want to suggest it as a patch…
I need to restrict the values available in a M2M dropdown field, while also using the autocomplete user interface. Overriding the field’s queryset
in the admin form’s __init__
only helps without autocomplete.
My workaround is to modify autocomplete.js
to return one extra query parameter with the ajax data:
extra_param: element.dataset.extraParam
…which I can stuff in via formfield_for_*
:
field.widget.attrs.update({‘data-extra-param’: myvalue})
…and retrieve via get_search_results
:
extra = request.GET['extra_param']
…thereby passing any needed extra context directly from form to query.
Adding this small patch to Django would allow any packaged app to do the same without overriding core code.