Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Jacob Kaplan-Moss authored May 14, 2009
28  django/contrib/admin/views/main.py
@@ -197,19 +197,21 @@ def get_query_set(self):
197 197
             raise IncorrectLookupParameters
198 198
 
199 199
         # Use select_related() if one of the list_display options is a field
200  
-        # with a relationship.
201  
-        if self.list_select_related:
202  
-            qs = qs.select_related()
203  
-        else:
204  
-            for field_name in self.list_display:
205  
-                try:
206  
-                    f = self.lookup_opts.get_field(field_name)
207  
-                except models.FieldDoesNotExist:
208  
-                    pass
209  
-                else:
210  
-                    if isinstance(f.rel, models.ManyToOneRel):
211  
-                        qs = qs.select_related()
212  
-                        break
  200
+        # with a relationship and the provided queryset doesn't already have
  201
+        # select_related defined.
  202
+        if not qs.query.select_related:
  203
+            if self.list_select_related:
  204
+                qs = qs.select_related()
  205
+            else:
  206
+                for field_name in self.list_display:
  207
+                    try:
  208
+                        f = self.lookup_opts.get_field(field_name)
  209
+                    except models.FieldDoesNotExist:
  210
+                        pass
  211
+                    else:
  212
+                        if isinstance(f.rel, models.ManyToOneRel):
  213
+                            qs = qs.select_related()
  214
+                            break
213 215
 
214 216
         # Set ordering.
215 217
         if self.order_field:
0  tests/regressiontests/admin_changelist/__init__.py
No changes.
9  tests/regressiontests/admin_changelist/models.py
... ...
@@ -0,0 +1,9 @@
  1
+from django.db import models
  2
+from django.contrib import admin
  3
+
  4
+class Parent(models.Model):
  5
+    name = models.CharField(max_length=128)
  6
+
  7
+class Child(models.Model):
  8
+    parent = models.ForeignKey(Parent, editable=False)
  9
+    name = models.CharField(max_length=30, blank=True)
24  tests/regressiontests/admin_changelist/tests.py
... ...
@@ -0,0 +1,24 @@
  1
+import unittest 
  2
+from django.contrib import admin
  3
+from django.contrib.admin.views.main import ChangeList
  4
+from regressiontests.admin_changelist.models import Child
  5
+
  6
+class ChangeListTests(unittest.TestCase):
  7
+    def test_select_related_preserved(self):
  8
+        """
  9
+        Regression test for #10348: ChangeList.get_query_set() shouldn't
  10
+        overwrite a custom select_related provided by ModelAdmin.queryset().
  11
+        """
  12
+        m = ChildAdmin(Child, admin.site)
  13
+        cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links, 
  14
+                m.list_filter,m.date_hierarchy, m.search_fields, 
  15
+                m.list_select_related, m.list_per_page, m.list_editable, m)
  16
+        self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
  17
+
  18
+class ChildAdmin(admin.ModelAdmin):
  19
+    list_display = ['name', 'parent']
  20
+    def queryset(self, request):
  21
+        return super(ChildAdmin, self).queryset(request).select_related("parent__name")
  22
+
  23
+class MockRequest(object):
  24
+    GET = {}

0 notes on commit 3da3716

Please sign in to comment.
Something went wrong with that request. Please try again.