Opened 7 months ago

Closed 6 months ago

Last modified 6 months ago

#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 Natalia Bidart, 7 months ago

Cc: Sarah Boyce added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

I can reproduce in latest Django main and I confirm that this is a regression in the reported revision 45ecd9acca9b36093e274f47b6877a5f79108d9e

comment:2 by Sarah Boyce, 7 months ago

Owner: changed from nobody to Sarah Boyce
Status: newassigned

comment:3 by Sarah Boyce, 7 months ago

Has patch: set

comment:4 by Natalia Bidart, 7 months ago

Needs documentation: set
Patch needs improvement: set

comment:5 by Sarah Boyce, 7 months ago

Needs documentation: unset
Patch needs improvement: unset

comment:6 by Mariusz Felisiak, 7 months ago

Triage Stage: AcceptedReady for checkin

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 6 months ago

Resolution: fixed
Status: assignedclosed

In f80669d:

Fixed #35020 -- Fixed ModelAdmin.lookup_allowed() for non-autofield primary keys.

Thanks Joshua Goodwin for the report.

Regression in 45ecd9acca9b36093e274f47b6877a5f79108d9e.

comment:8 by Mariusz Felisiak <felisiak.mariusz@…>, 6 months ago

In 454fd50e:

[5.0.x] Fixed #35020 -- Fixed ModelAdmin.lookup_allowed() for non-autofield primary keys.

Thanks Joshua Goodwin for the report.

Regression in 45ecd9acca9b36093e274f47b6877a5f79108d9e.

Backport of f80669d2f5a5f1db9e9b73ca893fefba34f955e7 from main

comment:9 by Aleksander Vognild Burkow, 6 months ago

Hi.

This seems to have broken django admin url filtering on foreign keys as well. I am on django 5.0.1.

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.

Last edited 6 months ago by Aleksander Vognild Burkow (previous) (diff)

comment:10 by Sarah Boyce, 6 months ago

Hi @aleksanb 👋
Yes, this has been raised here: https://code.djangoproject.com/ticket/35087

Note: See TracTickets for help on using tickets.
Back to Top