Opened 4 years ago

Closed 4 years ago

#32170 closed Uncategorized (needsinfo)

Admin select_related is different to the model's select related

Reported by: Michael Owned by: nobody
Component: Uncategorized Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Michael)

In a model one can do .select_related('foo__bar').

However in the admin view if one has select_related = ('foo__bar', ) it raises the error Non-relational field given in select_related: 'bar'. Choices are: .... One has to do list_select_related = ('foo', ). Hence to select only certain fields, one has to override the get_queryset.

Seems a bit inconsistent? I guess it goes hand in hand with not allowing foreign key fields in list display (#5863) etc.

Change History (2)

comment:1 by Michael, 4 years ago

Description: modified (diff)

comment:2 by Carlton Gibson, 4 years ago

Resolution: needsinfo
Status: newclosed

Hi Michael.

list_select_related is passed unchanged to the underlying `select_related()` method:

        if self.list_select_related:
            return qs.select_related(*self.list_select_related)

So it looks like it should work, but perhaps I've misunderstood you here:

However in the admin view if one has select_related = ('foo__bar', )...

Can I ask you to provide a sample project demonstrating the issue exactly, or ask that you add a test case to the Django test suite showing it, so we can assess?

Changelist tests, including those for list_select_related are in https://github.com/django/django/blob/master/tests/admin_changelist/tests.py

Thanks.

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