#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)
Change History (16)
comment:1 by , 16 years ago
Has patch: | set |
---|
comment:2 by , 16 years ago
comment:4 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 16 years ago
#10742 was a duplicate and proposed to use three-valued logic, leaving existing behaviour as-is and adding None
:
True
-- always callselect_related()
,False
(the default) -- callselect_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 , 16 years ago
Keywords: | efficient-admin added |
---|---|
Needs documentation: | set |
Needs tests: | set |
Patch needs improvement: | set |
by , 16 years ago
Attachment: | 10348_with-none-and-explicit-select-related.diff added |
---|
Add None and explicitly select the related fields when False
comment:7 by , 16 years ago
Cc: | 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 , 16 years ago
Attachment: | 10348-introspect-qs.diff added |
---|
Don't change select_related if it's already truthy. (with tests)
comment:8 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
The patch has a small error: s/selected/select