Opened 2 years ago

Last modified 3 months ago

#29707 assigned Cleanup/optimization

New admin autocomplete widget ignores limit_choices_to filter in referring FK definition

Reported by: David W. Lloyd Owned by: Johannes Hoppe
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: Carsten Fuchs, Étienne Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 - 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.

Change History (4)

comment:1 Changed 2 years ago by Carlton Gibson

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Version: 2.1master

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.

comment:2 Changed 2 years ago by Gianni Moschini

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:

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.

Last edited 2 years ago by Gianni Moschini (previous) (diff)

comment:3 Changed 3 months ago by Carsten Fuchs

Cc: Carsten Fuchs added

comment:4 Changed 3 months ago by Étienne

Cc: Étienne added
Note: See TracTickets for help on using tickets.
Back to Top