#35020 closed Bug (fixed)
DisallowedModelAdminLookup for foreign key to non-AutoField primary key
Reported by: | Joshua Goodwin | Owned by: | Sarah Boyce |
---|---|---|---|
Component: | contrib.admin | Version: | 5.0 |
Severity: | Release blocker | Keywords: | |
Cc: | Sarah Boyce | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I have a model whose primary key is not an AutoField, and another model with a foreign key pointing at that:
class Country(models.Model): id = models.CharField(max_length=2, primary_key=True) class Place(models.Model): country = models.ForeignKey(Country, models.CASCADE)
And a ModelAdmin with this list_filter:
@admin.register(models.Place) class PlaceAdmin(admin.ModelAdmin): list_filter = ["country"]
Since 45ecd9acca9b36093e274f47b6877a5f79108d9e, filtering places by country in the admin site raises django.contrib.admin.exceptions.DisallowedModelAdminLookup: Filtering by country__id__exact not allowed
Change History (10)
comment:1 by , 13 months ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 13 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 13 months ago
Has patch: | set |
---|
comment:4 by , 13 months ago
Needs documentation: | set |
---|---|
Patch needs improvement: | set |
comment:5 by , 13 months ago
Needs documentation: | unset |
---|---|
Patch needs improvement: | unset |
comment:6 by , 13 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 12 months ago
Hi.
This seems to have broken django admin url filtering on foreign keys as well.
I have a model, PriceGroup with FK to Booking, and when i attempt to filter PriceGroup in django admin on ?booking_id=... in the url i get Error: Filtering by booking not allowed
in a small red alert box.
The foreign key being declared as
booking = models.ForeignKey(Booking, null=False, on_delete=models.PROTECT)
and booking having the default primary key that django makes.
comment:10 by , 12 months ago
Hi @aleksanb 👋
Yes, this has been raised here: https://code.djangoproject.com/ticket/35087
I can reproduce in latest Django main and I confirm that this is a regression in the reported revision 45ecd9acca9b36093e274f47b6877a5f79108d9e