Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10348: ChangeList no longer overwrites a select_related provid…

…ed by `ModelAdmin.queryset()`.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10782 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3da371625272edb5af43446c32cbc7027223078d 1 parent d987b37
@jacobian jacobian authored
View
28 django/contrib/admin/views/main.py
@@ -197,19 +197,21 @@ def get_query_set(self):
raise IncorrectLookupParameters
# 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
+ # with a relationship and the provided queryset doesn't already have
+ # select_related defined.
+ if not qs.query.select_related:
+ 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:
View
0  tests/regressiontests/admin_changelist/__init__.py
No changes.
View
9 tests/regressiontests/admin_changelist/models.py
@@ -0,0 +1,9 @@
+from django.db import models
+from django.contrib import admin
+
+class Parent(models.Model):
+ name = models.CharField(max_length=128)
+
+class Child(models.Model):
+ parent = models.ForeignKey(Parent, editable=False)
+ name = models.CharField(max_length=30, blank=True)
View
24 tests/regressiontests/admin_changelist/tests.py
@@ -0,0 +1,24 @@
+import unittest
+from django.contrib import admin
+from django.contrib.admin.views.main import ChangeList
+from regressiontests.admin_changelist.models import Child
+
+class ChangeListTests(unittest.TestCase):
+ def test_select_related_preserved(self):
+ """
+ Regression test for #10348: ChangeList.get_query_set() shouldn't
+ overwrite a custom select_related provided by ModelAdmin.queryset().
+ """
+ m = ChildAdmin(Child, admin.site)
+ cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
+ m.list_filter,m.date_hierarchy, m.search_fields,
+ m.list_select_related, m.list_per_page, m.list_editable, m)
+ self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
+
+class ChildAdmin(admin.ModelAdmin):
+ list_display = ['name', 'parent']
+ def queryset(self, request):
+ return super(ChildAdmin, self).queryset(request).select_related("parent__name")
+
+class MockRequest(object):
+ GET = {}
Please sign in to comment.
Something went wrong with that request. Please try again.