Opened 14 hours ago

Closed 9 hours 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 :

  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)
  1. Have a simple list filter on that fk admin.py:
    class FooBarModelAdmin(ModelAdmin):
        model = FooBar
        list_filter = ("child",)
    
  1. 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.

Change History (1)

comment:1 by Sarah Boyce, 9 hours ago

Resolution: worksforme
Status: newclosed

Tested on main and 5.1, couldn't replicate the error

Note: See TracTickets for help on using tickets.
Back to Top