#21405 closed Bug (fixed)
Regression in ability to override queryset on BaseModelAdmin.formfield_for_foreignkey
| Reported by: | Owned by: | Claude Paroz | |
|---|---|---|---|
| Component: | contrib.admin | Version: | 1.6 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
After upgrading to Django 1.6, i have a problem with formfield_for_foreignkey.
This is my code:
class MatchScoreIntInline(admin.TabularInline):
model = MatchScoreInt
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "partial":
kwargs["queryset"] = Partial.objects.filter(...my filters...)
if db_field.name == "subscriber":
kwargs["queryset"] = Subscriber.objects.filter(...my filters...)
return super(MatchScoreIntInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
The super method override my kwargsqueryset, only for "subscriber" db_field because of this code:
queryset = self.get_field_queryset(db, db_field, request)
if queryset is not None:
kwargs['queryset'] = queryset
get_field_queryset method return a new ordered queryset if the related admin has "ordering" parameter.
So I removed the ordering parameter from my ModelAdmin and everything works fine.
Why I cannot override the queryset with formfield_for_foreignkey method when the related_admin has "ordering" parameter set?
Change History (4)
comment:1 by , 12 years ago
| Component: | Uncategorized → contrib.admin |
|---|---|
| Severity: | Normal → Release blocker |
| Summary: | formfield_for_foreignkey → Regression in ability to override queryset on BaseModelAdmin.formfield_for_foreignkey |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:2 by , 12 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:3 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
This regression was introduced in [d9330d5b]. It looks like the solution would be to avoid calling
get_field_queryset()if'queryset' in kwargs. This fix probably also applies toformfield_for_manytomany().The code from the second block in the ticket description is from django/contrib/admin/options.py.