Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10348: ChangeList no longer overwrites a select_relate…

…d provided by `ModelAdmin.queryset()`. Backport of [10782] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10783 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7f90dc1ba235e0050e6b73fff4f61ded3b297ccf 1 parent c293450
Jacob Kaplan-Moss authored May 14, 2009
28  django/contrib/admin/views/main.py
@@ -196,19 +196,21 @@ def get_query_set(self):
196 196
             raise IncorrectLookupParameters
197 197
 
198 198
         # Use select_related() if one of the list_display options is a field
199  
-        # with a relationship.
200  
-        if self.list_select_related:
201  
-            qs = qs.select_related()
202  
-        else:
203  
-            for field_name in self.list_display:
204  
-                try:
205  
-                    f = self.lookup_opts.get_field(field_name)
206  
-                except models.FieldDoesNotExist:
207  
-                    pass
208  
-                else:
209  
-                    if isinstance(f.rel, models.ManyToOneRel):
210  
-                        qs = qs.select_related()
211  
-                        break
  199
+        # with a relationship and the provided queryset doesn't already have
  200
+        # select_related defined.
  201
+        if not qs.query.select_related:
  202
+            if self.list_select_related:
  203
+                qs = qs.select_related()
  204
+            else:
  205
+                for field_name in self.list_display:
  206
+                    try:
  207
+                        f = self.lookup_opts.get_field(field_name)
  208
+                    except models.FieldDoesNotExist:
  209
+                        pass
  210
+                    else:
  211
+                        if isinstance(f.rel, models.ManyToOneRel):
  212
+                            qs = qs.select_related()
  213
+                            break
212 214
 
213 215
         # Set ordering.
214 216
         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)
  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 7f90dc1

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