[Django] #35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed in list_filters

58 views
Skip to first unread message

Django

unread,
Jan 5, 2024, 7:01:59 AM1/5/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime | Owner: nobody
Lorant |
Type: Bug | Status: new
Component: | Version: 5.0
contrib.admin | Keywords: admin filters
Severity: Normal | foreignkey
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Since Django 5.0.1, the exception `DisallowedModelAdminLookup` is raised
when I'm trying to filter a list in the Django admin with a foreign key
attribute not listed in `list_filters`. It looks like a regression from
#35020, but I'm not exactly sure at 100 %. The exception is not raised
when I'm filtering against a foreign key attribute listed in
`list_filters` neither for a standard CharField not listed. Using the
following simple example, on a fresh new Django project:


{{{
# models.py
class Client(models.Model):
name = models.CharField("name", max_length=100)

class Invoice(models.Model):
client = models.ForeignKey(Client, models.CASCADE, null=False)
number = models.CharField("number", max_length=100)

# admin.py
@admin.register(models.Invoice)
class InvoiceAdmin(admin.ModelAdmin):
pass # note that no list_filters defined
}}}

The URL `/admin/core/invoice/?client=1` in local:

- returns a 200, with proper results in Django 4.2.9
- returns a 200, with proper results in Django 5.0.0
- returns a 500, from the exception below in Django 5.0.1

The exception raised in Django 5.0.1:

{{{
File "<VENV>/lib/python3.12/site-
packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
[...]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<VENV>/lib/python3.12/site-
packages/django/contrib/admin/options.py", line 1981, in changelist_view
cl = self.get_changelist_instance(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<VENV>/lib/python3.12/site-
packages/django/contrib/admin/options.py", line 862, in
get_changelist_instance
return ChangeList(
^^^^^^^^^^^
File "<VENV>/lib/python3.12/site-
packages/django/contrib/admin/views/main.py", line 144, in __init__
self.queryset = self.get_queryset(request)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<VENV>/lib/python3.12/site-
packages/django/contrib/admin/views/main.py", line 539, in get_queryset
) = self.get_filters(request)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "<VENV>/lib/python3.12/site-
packages/django/contrib/admin/views/main.py", line 193, in get_filters
raise DisallowedModelAdminLookup(f"Filtering by {key} not allowed")
django.contrib.admin.exceptions.DisallowedModelAdminLookup: Filtering by
client not allowed
}}}

The URL `/admin/core/invoice/?number=ABC` works in the three versions with
the same behaviour each time (make an exact match on the charfield)

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

Django

unread,
Jan 5, 2024, 7:05:39 AM1/5/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Normal | Resolution:
Keywords: admin filters | Triage Stage:
foreignkey | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Maxime Lorant:

Old description:

New description:

Since Django 5.0.1, the exception `DisallowedModelAdminLookup` is raised
when I'm trying to filter a list in the Django admin with a foreign key
attribute not listed in `list_filters`. It looks like a regression from
#35020, but I'm not exactly sure at 100 %. The exception is not raised
when I'm filtering against a foreign key attribute listed in
`list_filters` neither for a standard CharField not listed. Using the
following simple example, on a fresh new Django project:


{{{
# models.py
class Client(models.Model):
name = models.CharField("name", max_length=100)

class Invoice(models.Model):
client = models.ForeignKey(Client, models.CASCADE, null=False)
number = models.CharField("number", max_length=100)

# admin.py
@admin.register(models.Invoice)
class InvoiceAdmin(admin.ModelAdmin):

pass # note that no list_filters is defined
}}}

--

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

Django

unread,
Jan 5, 2024, 7:23:30 AM1/5/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: admin filters | Triage Stage: Accepted
foreignkey |

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

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


Comment:

Thanks for the report!

Regression in f80669d2f5a5f1db9e9b73ca893fefba34f955e7.

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

Django

unread,
Jan 5, 2024, 8:14:40 AM1/5/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: Sarah
| Boyce
Type: Bug | Status: assigned

Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: admin filters | Triage Stage: Accepted
foreignkey |
Has patch: 1 | 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
* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/17700 👍

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

Django

unread,
Jan 8, 2024, 3:39:23 AM1/8/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution:
Keywords: admin filters | Triage Stage: Ready for
foreignkey | 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/35087#comment:4>

Django

unread,
Jan 8, 2024, 8:25:32 AM1/8/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed

Keywords: admin filters | Triage Stage: Ready for
foreignkey | 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:"a9094ec1f43dca7f2a649327afcd5e6226b4959c" a9094ec1]:
{{{
#!CommitTicketReference repository=""
revision="a9094ec1f43dca7f2a649327afcd5e6226b4959c"
Fixed #35087 -- Reallowed filtering against foreign keys not listed in
ModelAdmin.list_filters.

Regression in f80669d2f5a5f1db9e9b73ca893fefba34f955e7.
}}}

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

Django

unread,
Jan 8, 2024, 8:25:56 AM1/8/24
to django-...@googlegroups.com
#35087: DisallowedModelAdminLookup raised when filtering on a ForeignKey not listed
in list_filters
-------------------------------------+-------------------------------------
Reporter: Maxime Lorant | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.0
Severity: Release blocker | Resolution: fixed
Keywords: admin filters | Triage Stage: Ready for
foreignkey | 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:"4cba6748a640a158a26a0a0d11597b393bdd3883" 4cba6748]:
{{{
#!CommitTicketReference repository=""
revision="4cba6748a640a158a26a0a0d11597b393bdd3883"
[5.0.x] Fixed #35087 -- Reallowed filtering against foreign keys not
listed in ModelAdmin.list_filters.

Regression in f80669d2f5a5f1db9e9b73ca893fefba34f955e7.

Backport of a9094ec1f43dca7f2a649327afcd5e6226b4959c from main
}}}

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

Reply all
Reply to author
Forward
0 new messages