[Django] #35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key

39 views
Skip to first unread message

Django

unread,
Dec 6, 2023, 7:17:16 AM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
------------------------------------------+------------------------
Reporter: Joshua Goodwin | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
I have a model whose primary key is not an AutoField, and another model
with a foreign key pointing at that:

{{{#!python
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:

{{{#!python
@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`

--
Ticket URL: <https://code.djangoproject.com/ticket/35020>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 6, 2023, 8:40:39 AM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
---------------------------------+------------------------------------

Reporter: Joshua Goodwin | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Natalia Bidart):

* cc: Sarah Boyce (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:1>

Django

unread,
Dec 6, 2023, 10:36:37 AM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
---------------------------------+---------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah Boyce
Type: Bug | Status: assigned

Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* owner: nobody => Sarah Boyce
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:2>

Django

unread,
Dec 6, 2023, 2:13:10 PM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
---------------------------------+---------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah Boyce
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:3>

Django

unread,
Dec 6, 2023, 3:36:29 PM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
---------------------------------+---------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah Boyce
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1
* needs_docs: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:4>

Django

unread,
Dec 6, 2023, 4:55:36 PM12/6/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
---------------------------------+---------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah Boyce
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0
* needs_docs: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:5>

Django

unread,
Dec 7, 2023, 12:13:07 AM12/7/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
-------------------------------------+-------------------------------------

Reporter: Joshua Goodwin | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:6>

Django

unread,
Dec 7, 2023, 2:45:00 AM12/7/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
-------------------------------------+-------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"f80669d2f5a5f1db9e9b73ca893fefba34f955e7" f80669d]:
{{{
#!CommitTicketReference repository=""
revision="f80669d2f5a5f1db9e9b73ca893fefba34f955e7"
Fixed #35020 -- Fixed ModelAdmin.lookup_allowed() for non-autofield
primary keys.

Thanks Joshua Goodwin for the report.

Regression in 45ecd9acca9b36093e274f47b6877a5f79108d9e.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:7>

Django

unread,
Dec 7, 2023, 2:45:17 AM12/7/23
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
-------------------------------------+-------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"454fd50efb4b6bba54abdbcc01507190c780382f" 454fd50e]:
{{{
#!CommitTicketReference repository=""
revision="454fd50efb4b6bba54abdbcc01507190c780382f"
[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
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:8>

Django

unread,
Jan 5, 2024, 8:52:14 AM1/5/24
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
-------------------------------------+-------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by aleksanb):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:9>

Django

unread,
Jan 5, 2024, 8:55:29 AM1/5/24
to django-...@googlegroups.com
#35020: DisallowedModelAdminLookup for foreign key to non-AutoField primary key
-------------------------------------+-------------------------------------
Reporter: Joshua Goodwin | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Sarah Boyce):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/35020#comment:10>

Reply all
Reply to author
Forward
0 new messages