Opened 10 months ago
Closed 10 months ago
#36126 closed Bug (worksforme)
DisallowedModelAdminLookup on simple filters of FK fields with inheritance child model as target
| Reported by: | Omar BENHAMID | Owned by: | |
|---|---|---|---|
| Component: | contrib.admin | Version: | 5.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Omar BENHAMID | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
To reproduce :
- 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)
- Have a simple list filter on that fk admin.py:
class FooBarModelAdmin(ModelAdmin): model = FooBar list_filter = ("child",)
- 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__exactto avoid exposing some "unnecessary" technical detail, but maybe it has side effects, so the default lookup_allowed(...) should at least allow it.
Note:
See TracTickets
for help on using tickets.
Tested on main and 5.1, couldn't replicate the error