[Django] #32170: Admin select_related is different to the model's select related

6 views
Skip to first unread message

Django

unread,
Nov 4, 2020, 9:10:43 AM11/4/20
to django-...@googlegroups.com
#32170: Admin select_related is different to the model's select related
-----------------------------------------+------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.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 |
-----------------------------------------+------------------------
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 `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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32170>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 4, 2020, 9:42:20 AM11/4/20
to django-...@googlegroups.com
#32170: Admin select_related is different to the model's select related
-------------------------------+--------------------------------------

Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Michael:

Old description:

> 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 `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.

New description:

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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32170#comment:1>

Django

unread,
Nov 4, 2020, 2:57:57 PM11/4/20
to django-...@googlegroups.com
#32170: Admin select_related is different to the model's select related
-------------------------------+--------------------------------------
Reporter: Michael | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: needsinfo

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 Carlton Gibson):

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


Comment:

Hi Michael.

`list_select_related` is
[https://github.com/django/django/blob/ebb08d19424c314c75908bc6048ff57c2f872269/django/contrib/admin/views/main.py#L504-L505
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32170#comment:2>

Reply all
Reply to author
Forward
0 new messages