8000 [5.0.x] Fixed #35173 -- Fixed ModelAdmin.lookup_allowed() for lookups… · django/django@3a54e64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a54e64

Browse files
Hisham-Paknessita
authored andcommitted
[5.0.x] Fixed #35173 -- Fixed ModelAdmin.lookup_allowed() for lookups on foreign keys when not included in ModelAdmin.list_filter.
Regression in f80669d. Thanks Sarah Boyce for the review. Backport of 8db593d from main
1 parent 1ba5afa commit 3a54e64

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

django/contrib/admin/options.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,24 +473,25 @@ def lookup_allowed(self, lookup, value, request=None):
473473
# Lookups on nonexistent fields are ok, since they're ignored
474474
# later.
475475
break
476+
if not prev_field or (
477+
prev_field.is_relation
478+
and field not in model._meta.parents.values()
479+
and field is not model._meta.auto_field
480+
a 8000 nd (
481+
model._meta.auto_field is None
482+
or part not in getattr(prev_field, "to_fields", [])
483+
)
484+
and (field.is_relation or not field.primary_key)
485+
):
486+
relation_parts.append(part)
476487
if not getattr(field, "path_infos", None):
477488
# This is not a relational field, so further parts
478489
# must be transforms.
479490
break
480-
if (
481-
not prev_field
482-
or (field.is_relation and field not in model._meta.parents.values())
483-
or (
484-
prev_field.is_relation
485-
and model._meta.auto_field is None
486-
and part not in getattr(prev_field, "to_fields", [])
487-
)
488-
):
489-
relation_parts.append(part)
490491
prev_field = field
491492
model = field.path_infos[-1].to_opts.model
492493

493-
if not relation_parts or len(parts) == 1:
494+
if len(relation_parts) <= 1:
494495
# Either a local field filter, or no fields at all.
495496
return True
496497
valid_lookups = {self.date_hierarchy}

docs/releases/5.0.3.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ Bugfixes
1515
* Fixed a bug in Django 5.0 that caused a crash of ``Signal.asend()`` and
1616
``asend_robust()`` when all receivers were asynchronous functions
1717
(:ticket:`35174`).
18+
19+
* Fixed a regression in Django 5.0.1 where :meth:`.ModelAdmin.lookup_allowed`
20+
would prevent filtering against foreign keys using lookups like ``__isnull``
21+
when the field was not included in :attr:`.ModelAdmin.list_filter`
22+
(:ticket:`35173`).

tests/modeladmin/tests.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,19 @@ class PlaceAdmin(ModelAdmin):
174174
pass
175175

176176
ma = PlaceAdmin(Place, self.site)
177-
self.assertIs(ma.lookup_allowed("country", "1", request), True)
177+
178+
cases = [
179+
("country", "1"),
180+
("country__exact", "1"),
181+
("country__id", "1"),
182+
("country__id__exact", "1"),
183+
("country__isnull", True),
184+
("country__isnull", False),
185+
("country__id__isnull", False),
186+
]
187+
for lookup, lookup_value in cases:
188+
with self.subTest(lookup=lookup):
189+
self.assertIs(ma.lookup_allowed(lookup, lookup_value, request), True)
178190

179191
@isolate_apps("modeladmin")
180192
def test_lookup_allowed_non_autofield_primary_key(self):

0 commit comments

Comments
 (0)
0