Ticket #14396: 14396_list_select_related.diff
File 14396_list_select_related.diff, 7.6 KB (added by , 14 years ago) |
---|
-
django/contrib/admin/validation.py
4 4 _get_foreign_key) 5 5 from django.contrib.admin.options import flatten_fieldsets, BaseModelAdmin 6 6 from django.contrib.admin.options import HORIZONTAL, VERTICAL 7 from django.contrib.admin.util import lookup_field 7 8 8 9 9 10 __all__ = ['validate'] … … 132 133 raise ImproperlyConfigured("%s.readonly_fields[%d], %r is not a callable or an attribute of %r or found in the model %r." 133 134 % (cls.__name__, idx, field, cls.__name__, model._meta.object_name)) 134 135 135 # list_select_related = False136 136 # save_as = False 137 137 # save_on_top = False 138 for attr in (' list_select_related', 'save_as', 'save_on_top'):138 for attr in ('save_as', 'save_on_top'): 139 139 if not isinstance(getattr(cls, attr), bool): 140 140 raise ImproperlyConfigured("'%s.%s' should be a boolean." 141 141 % (cls.__name__, attr)) 142 142 143 # list_select_related = False 144 if not isinstance(cls.list_select_related, (bool, list, tuple)): 145 raise ImproperlyConfigured("'%s.list_select_related' should be a boolean, a list or a tuple." 146 % cls.__name__) 143 147 144 148 # inlines = [] 145 149 if hasattr(cls, 'inlines'): … … 169 173 fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True) 170 174 171 175 # extra = 3 172 if not isinstance( cls.extra, int):176 if not isinstance(getattr(cls, 'extra'), int): 173 177 raise ImproperlyConfigured("'%s.extra' should be a integer." 174 178 % cls.__name__) 175 179 -
django/contrib/admin/views/main.py
3 3 from django.contrib.admin.util import quote 4 4 from django.core.paginator import Paginator, InvalidPage 5 5 from django.db import models 6 from django.db.models.query import QuerySet 6 7 from django.utils.encoding import force_unicode, smart_str 7 8 from django.utils.translation import ugettext 8 9 from django.utils.http import urlencode … … 201 202 # with a relationship and the provided queryset doesn't already have 202 203 # select_related defined. 203 204 if not qs.query.select_related: 204 if self.list_select_related: 205 if isinstance(self.list_select_related, (list, tuple)): 206 qs = qs.select_related(*self.list_select_related) 207 elif self.list_select_related: 205 208 qs = qs.select_related() 206 209 else: 207 210 for field_name in self.list_display: … … 210 213 except models.FieldDoesNotExist: 211 214 pass 212 215 else: 213 if isinstance(f.rel, models.ManyToOneRel): 214 qs = qs.select_related() 215 break 216 if isinstance(f.rel, (models.ManyToOneRel, models.OneToOneRel)): 217 qs = qs.select_related(field_name) 216 218 217 219 # Set ordering. 218 220 if self.order_field: -
tests/regressiontests/admin_changelist/tests.py
2 2 from django.contrib.admin.views.main import ChangeList 3 3 from django.template import Context, Template 4 4 from django.test import TransactionTestCase 5 from regressiontests.admin_changelist.models import Child, Parent 5 from regressiontests.admin_changelist.models import Child, Parent, NullableChild 6 6 7 7 class ChangeListTests(TransactionTestCase): 8 8 def test_select_related_preserved(self): … … 16 16 m.list_select_related, m.list_per_page, m.list_editable, m) 17 17 self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}}) 18 18 19 def test_select_related_nullable(self): 20 """ 21 Regression test for #14396: allow list_select_related to explicitly 22 specify relations to follow. 23 """ 24 m = NullableChildAdmin(NullableChild, admin.site) 25 cl = ChangeList(MockRequest(), NullableChild, m.list_display, 26 m.list_display_links, m.list_filter, m.date_hierarchy, 27 m.search_fields, m.list_select_related, m.list_per_page, 28 m.list_editable, m) 29 self.assertEqual(cl.query_set.query.select_related, {'parent': {}}) 30 19 31 def test_result_list_html(self): 20 32 """ 21 33 Verifies that inclusion tag result_list generates a table when with … … 76 88 def queryset(self, request): 77 89 return super(ChildAdmin, self).queryset(request).select_related("parent__name") 78 90 91 class NullableChildAdmin(admin.ModelAdmin): 92 list_select_related = ('parent',) 93 79 94 class MockRequest(object): 80 95 GET = {} -
tests/regressiontests/admin_changelist/models.py
6 6 7 7 class Child(models.Model): 8 8 parent = models.ForeignKey(Parent, editable=False) 9 name = models.CharField(max_length=30, blank=True) 10 No newline at end of file 9 name = models.CharField(max_length=30, blank=True) 10 11 class NullableChild(models.Model): 12 parent = models.ForeignKey(Parent, null=True, blank=True) 13 name = models.CharField(max_length=30, blank=True) -
tests/regressiontests/modeladmin/tests.py
963 963 964 964 self.assertRaisesRegexp( 965 965 ImproperlyConfigured, 966 "'ValidationTestModelAdmin.list_select_related' should be a boolean .",966 "'ValidationTestModelAdmin.list_select_related' should be a boolean, a list or a tuple.", 967 967 validate, 968 968 ValidationTestModelAdmin, 969 969 ValidationTestModel, … … 974 974 975 975 validate(ValidationTestModelAdmin, ValidationTestModel) 976 976 977 class ValidationTestModelAdmin(ModelAdmin): 978 list_select_related = ('field1', 'field2') 979 980 validate(ValidationTestModelAdmin, ValidationTestModel) 981 977 982 def test_save_as_validation(self): 978 983 979 984 class ValidationTestModelAdmin(ModelAdmin): -
docs/ref/contrib/admin/index.txt
476 476 objects on the admin change list page. This can save you a bunch of database 477 477 queries. 478 478 479 The value should be either ``True`` or ``False``. Default is ``False``.479 The value should be one of: 480 480 481 * ``True`` or ``False``. If ``True``, the standard ``select_related()`` call 482 will be used. 483 484 * A list or tuple of field names. In this case, the value is used as the 485 argument to :meth:`~django.db.models.QuerySet.select_related` which determines 486 which relations should be followed. 487 488 The default is ``False``. 489 481 490 Note that Django will use :meth:`~django.db.models.QuerySet.select_related`, 482 491 regardless of this setting, if one of the ``list_display`` fields is a 483 ``ForeignKey`` .492 ``ForeignKey`` or ``OneToOneField``. 484 493 485 494 .. attribute:: ModelAdmin.inlines 486 495