Opened 6 years ago
Last modified 4 years ago
#29707 assigned Cleanup/optimization
New admin autocomplete widget ignores limit_choices_to filter in referring FK definition
Description ¶
Standard dropdowns and widgets respect limit_choices_to in ORM definition and thus filter available choices accordingly, but the new autocomplete widget does not, providing a counter-intuitive/inconsistent experience.
See https://code.djangoproject.com/ticket/29010#comment:15 - these two tickets are related in the sense that a fix to this bug *may* also introduce a means of the autocomplete widget/view interaction knowing what the referring model is, one way or another.
According to the ticket's flags, the next step(s) to move this issue forward are:
- To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.
Change History (6)
comment:1 by , 6 years ago
Triage Stage: | Unreviewed → Accepted |
---|---|
Type: | Bug → Cleanup/optimization |
Version: | 2.1 → master |
comment:2 by , 6 years ago
I've had the same issue, and tried to fix it using proxy models (to customise the queryset). Unfortunately modifying the field model to the proxy is not an option, so had to use hacks to replace the widget/model, first via formfield_for_dbfield, but that creates a bunch of issues.
Again, unfortunately the only way I found that worked was to overwrite the widget for each field in formfield_for_foreignkey/formfield_for_manytomany.
But again, using the proxy model causes issues, like having to create a new model and register it through a new ModelAdmin class, which can be an issue if this is to be supported within django.
Finally I found that it's possible to customise the AutocompleteJsonView, which is fairly easy to do.
Here is a snippet of what I've done so far:
https://gist.github.com/leibowitz/7a237ea8151f2d238b19ec0da5db545c/c0576bb843e80c3c900ffab3e5636150585256a6
Not sure if that's useful, but I can transform that to a patch into django ModelAdmin and AutocompleteJsonView classes, if that's good enough.
Note that the whole widget customisation wouldn't be needed in that case, obviously.
comment:3 by , 5 years ago
Cc: | added |
---|
comment:4 by , 5 years ago
Cc: | added |
---|
comment:5 by , 4 years ago
Cc: | added |
---|
comment:6 by , 4 years ago
Considering the case of an FK from choice to poll called my_poll. It would've been nice if the autocomplete call was choice/autocomplete?field=my_poll&term=bob
instead of poll/autocomplete?term=bob
that would have made context based filtering way way easier.
OK, lets accept this given the previous discussion on #29010. (Joe, I'm assuming you're happy to take this on: if not please un-assign.)
We'll re-assess #29010 after this.