Opened 16 years ago

Closed 15 years ago

Last modified 5 years ago

#10348 closed (fixed)

contrib.admin: select_related overwritten by django.contrib.admin.views.ChangeList.get_query_set

Reported by: erny Owned by: nobody
Component: contrib.admin Version: dev
Severity: Keywords: efficient-admin
Cc: matthew@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

In trunk and 1.0.x, although I define (see #8213, see also #9554, and #9849 marked as dupe)

class MyModelAdmin(ModelAdmin):
    def queryset(self):
        return super(MyModelAdmin, self).queryset().select_related('foo__bar__baz', depth=2)

it is completely overwritten by the following code in `django/contrib/admin/views/main.py near line 83:

        # Use select_related() if one of the list_display options is a field
        # with a relationship.
        if self.list_select_related:
            qs = qs.select_related()
        else:
            for field_name in self.list_display:
                try:
                    f = self.lookup_opts.get_field(field_name)
                except models.FieldDoesNotExist:
                    pass
                else:
                    if isinstance(f.rel, models.ManyToOneRel):
                        qs = qs.select_related()
                        break

        # Set ordering.
        if self.order_field:
            qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field))

This should definitively be an option of ModelAdmin.

Attachments (3)

admin_select_related.diff (1.5 KB ) - added by erny 16 years ago.
new version of patch
10348_with-none-and-explicit-select-related.diff (2.1 KB ) - added by mrts 16 years ago.
Add None and explicitly select the related fields when False
10348-introspect-qs.diff (3.8 KB ) - added by mmarshall 16 years ago.
Don't change select_related if it's already truthy. (with tests)

Download all attachments as: .zip

Change History (16)

comment:1 by erny, 16 years ago

Has patch: set

comment:2 by erny, 16 years ago

The patch has a small error: s/selected/select

comment:3 by erny, 16 years ago

just found another error, will attach new patch.

by erny, 16 years ago

Attachment: admin_select_related.diff added

new version of patch

comment:4 by Jacob, 16 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:5 by mrts, 16 years ago

#10742 was a duplicate and proposed to use three-valued logic, leaving existing behaviour as-is and adding None:

  • True -- always call select_related(),
  • False (the default) -- call select_related() if any of the fields in list_display is a foreign key,
  • None (added) -- leave the queryset alone.

It has a working patch.

comment:6 by mrts, 16 years ago

Keywords: efficient-admin added
Needs documentation: set
Needs tests: set
Patch needs improvement: set

by mrts, 16 years ago

Add None and explicitly select the related fields when False

comment:7 by mmarshall, 16 years ago

Cc: matthew@… added

Distinguishing between False and None feels hacky to me.

Would it work to have ChangeList.get_query_set only call select_related if qs.query.select_related is False?

by mmarshall, 16 years ago

Attachment: 10348-introspect-qs.diff added

Don't change select_related if it's already truthy. (with tests)

comment:8 by Jacob, 15 years ago

Resolution: fixed
Status: newclosed

(In [10782]) Fixed #10348: ChangeList no longer overwrites a select_related provided by ModelAdmin.queryset().

comment:9 by Jacob, 15 years ago

(In [10783]) [1.0.X] Fixed #10348: ChangeList no longer overwrites a select_related provided by ModelAdmin.queryset(). Backport of [10782] from trunk.

comment:10 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

comment:11 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Easy pickings: unset
UI/UX: unset

In 3ea957b5:

[3.0.x] Refs #10348 -- Doc'd that ModelAdmin ignores list_select_related when QuerySet.select_related() was already called.

Backport of e3f647f4d5fc414f8cb7ab58df5e61a19908c63f from master

comment:12 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In e3f647f:

Refs #10348 -- Doc'd that ModelAdmin ignores list_select_related when QuerySet.select_related() was already called.

comment:13 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In 98573cfe:

[2.2.x] Refs #10348 -- Doc'd that ModelAdmin ignores list_select_related when QuerySet.select_related() was already called.

Backport of e3f647f4d5fc414f8cb7ab58df5e61a19908c63f from master

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