[Django] #36126: DisallowedModelAdminLookup on simple filters of FK fields with inheritance child model as target

9 views
Skip to first unread message

Django

unread,
Jan 23, 2025, 5:56:16 AM1/23/25
to django-...@googlegroups.com
#36126: DisallowedModelAdminLookup on simple filters of FK fields with inheritance
child model as target
-------------------------------+-----------------------------------------
Reporter: Omar BENHAMID | Type: Bug
Status: new | Component: contrib.admin
Version: 5.1 | 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
-------------------------------+-----------------------------------------
To reproduce :

1. Have an FK to a model which inherits another in models.py :

{{{
class Parent(Model):
#...
pass


class Child(Parent):
#...
pass

class FooBar(Model):
child = ForeignKey(Child, on_delete=models.CASCADE)
}}}

2. Have a simple list filter on that fk admin.py:
{{{
class FooBarModelAdmin(ModelAdmin):
model = FooBar
list_filter = ("child",)
}}}

3. Go to the admin of FooBar and try to filter by "child" you get :
{{{
django.contrib.admin.exceptions.DisallowedModelAdminLookup: Filtering by
child__parent_ptr__exact not allowed
}}}

**Workaround:** override {{{lookup_allowed(...)}}} in the
FooBarModelAdmin to allow lookup on {{{child__parent_ptr__exact}}}.

I think that the better fix would be to filter by child_exact rather than
{{{child__parent_ptr__exact}}}to avoid exposing some "unnecessary"
technical detail, but maybe it has side effects, so the default
{{{lookup_allowed(...)}}} should at least allow it.
--
Ticket URL: <https://code.djangoproject.com/ticket/36126>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 23, 2025, 10:29:39 AM1/23/25
to django-...@googlegroups.com
#36126: DisallowedModelAdminLookup on simple filters of FK fields with inheritance
child model as target
-------------------------------+--------------------------------------
Reporter: Omar BENHAMID | Owner: (none)
Type: Bug | Status: closed
Component: contrib.admin | Version: 5.1
Severity: Normal | Resolution: worksforme
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => worksforme
* status: new => closed

Comment:

Tested on main and 5.1, couldn't replicate the error
--
Ticket URL: <https://code.djangoproject.com/ticket/36126#comment:1>
Reply all
Reply to author
Forward
0 new messages