Fixed #10348: ChangeList no longer overwrites a select_related provided by `ModelAdmin.queryset()`.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10782 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2009-05-14 15:09:33 +00:00
parent d987b378ce
commit 3da3716252
4 changed files with 48 additions and 13 deletions

View File

@ -197,19 +197,21 @@ class ChangeList(object):
raise IncorrectLookupParameters raise IncorrectLookupParameters
# Use select_related() if one of the list_display options is a field # Use select_related() if one of the list_display options is a field
# with a relationship. # with a relationship and the provided queryset doesn't already have
if self.list_select_related: # select_related defined.
qs = qs.select_related() if not qs.query.select_related:
else: if self.list_select_related:
for field_name in self.list_display: qs = qs.select_related()
try: else:
f = self.lookup_opts.get_field(field_name) for field_name in self.list_display:
except models.FieldDoesNotExist: try:
pass f = self.lookup_opts.get_field(field_name)
else: except models.FieldDoesNotExist:
if isinstance(f.rel, models.ManyToOneRel): pass
qs = qs.select_related() else:
break if isinstance(f.rel, models.ManyToOneRel):
qs = qs.select_related()
break
# Set ordering. # Set ordering.
if self.order_field: if self.order_field:

View File

@ -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 File

@ -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 = {}