Ticket #14396: 14396_list_select_related2.patch
File 14396_list_select_related2.patch, 6.7 KB (added by , 14 years ago) |
---|
-
docs/ref/contrib/admin/index.txt
657 657 .. attribute:: ModelAdmin.list_select_related 658 658 659 659 Set ``list_select_related`` to tell Django to use 660 :meth:`~django.db.models. QuerySet.select_related` in retrieving the list of661 objects on the admin change list page. This can save you a bunch of660 :meth:`~django.db.models.query.QuerySet.select_related` in retrieving the 661 list of objects on the admin change list page. This can save you a bunch of 662 662 database queries. 663 663 664 The value should be either ``True`` or ``False``. Default is ``False``.664 The value should be one of: 665 665 666 Note that Django will use :meth:`~django.db.models.QuerySet.select_related`, 667 regardless of this setting if one of the ``list_display`` fields is a 668 ``ForeignKey``. 666 * ``True`` or ``False``. If ``True``, the standard 667 :meth:`~django.db.models.query.QuerySet.select_related` call will be used. 669 668 669 * A list or tuple of field names. In this case, the value is used as the 670 argument to :meth:`~django.db.models.query.QuerySet.select_related` which 671 determines which relations should be followed. 672 673 The default is ``False``. 674 675 Note that Django will use 676 :meth:`~django.db.models.query.QuerySet.select_related`, regardless of this 677 setting, if one of the :attr:`~ModelAdmin.list_display` fields is a 678 :class:`~django.db.models.ForeignKey` or 679 :class:`~django.db.models.OneToOneField`. 680 670 681 .. attribute:: ModelAdmin.ordering 671 682 672 683 Set ``ordering`` to specify how lists of objects should be ordered in the -
django/contrib/admin/validation.py
160 160 if hasattr(cls, "readonly_fields"): 161 161 check_readonly_fields(cls, model, opts) 162 162 163 # list_select_related = False164 163 # save_as = False 165 164 # save_on_top = False 166 for attr in (' list_select_related', 'save_as', 'save_on_top'):165 for attr in ('save_as', 'save_on_top'): 167 166 if not isinstance(getattr(cls, attr), bool): 168 167 raise ImproperlyConfigured("'%s.%s' should be a boolean." 169 168 % (cls.__name__, attr)) 170 169 170 # list_select_related = False 171 if not isinstance(cls.list_select_related, (bool, list, tuple)): 172 raise ImproperlyConfigured("'%s.list_select_related' should be a boolean, a list or a tuple." 173 % cls.__name__) 171 174 172 175 # inlines = [] 173 176 if hasattr(cls, 'inlines'): … … 197 200 fk = _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name, can_fail=True) 198 201 199 202 # extra = 3 200 if not isinstance( cls.extra, int):203 if not isinstance(getattr(cls, 'extra'), int): 201 204 raise ImproperlyConfigured("'%s.extra' should be a integer." 202 205 % cls.__name__) 203 206 -
django/contrib/admin/views/main.py
3 3 from django.core.exceptions import SuspiciousOperation 4 4 from django.core.paginator import 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, ugettext_lazy 8 9 from django.utils.http import urlencode … … 276 277 # with a relationship and the provided queryset doesn't already have 277 278 # select_related defined. 278 279 if not qs.query.select_related: 279 if self.list_select_related: 280 if isinstance(self.list_select_related, (list, tuple)): 281 qs = qs.select_related(*self.list_select_related) 282 elif self.list_select_related: 280 283 qs = qs.select_related() 281 284 else: 282 285 for field_name in self.list_display: … … 285 288 except models.FieldDoesNotExist: 286 289 pass 287 290 else: 288 if isinstance(field.rel, models.ManyToOneRel):291 if isinstance(field.rel, (models.ManyToOneRel, models.OneToOneRel)): 289 292 qs = qs.select_related() 290 293 break 291 294 -
tests/regressiontests/admin_changelist/tests.py
41 41 'Failed to find expected row element: %s' % table_output) 42 42 43 43 44 def test_select_related_nullable(self): 45 """ 46 Regression test for #14396: allow list_select_related to explicitly 47 specify relations to follow. 48 """ 49 m = NullableChildAdmin(Child, admin.site) 50 cl = ChangeList(MockRequest(), Child, m.list_display, 51 m.list_display_links, m.list_filter, m.date_hierarchy, 52 m.search_fields, m.list_select_related, m.list_per_page, 53 m.list_editable, m) 54 self.assertEqual(cl.query_set.query.select_related, {'parent': {}}) 55 44 56 def test_result_list_html(self): 45 57 """ 46 58 Verifies that inclusion tag result_list generates a table when with … … 321 333 return super(FilteredChildAdmin, self).queryset(request).filter( 322 334 name__contains='filtered') 323 335 336 class NullableChildAdmin(admin.ModelAdmin): 337 list_select_related = ('parent',) 338 324 339 class MockRequest(object): 325 340 GET = {} 326 341 -
tests/regressiontests/modeladmin/tests.py
1032 1032 1033 1033 self.assertRaisesRegexp( 1034 1034 ImproperlyConfigured, 1035 "'ValidationTestModelAdmin.list_select_related' should be a boolean .",1035 "'ValidationTestModelAdmin.list_select_related' should be a boolean, a list or a tuple.", 1036 1036 validate, 1037 1037 ValidationTestModelAdmin, 1038 1038 ValidationTestModel, … … 1043 1043 1044 1044 validate(ValidationTestModelAdmin, ValidationTestModel) 1045 1045 1046 class ValidationTestModelAdmin(ModelAdmin): 1047 list_select_related = ('field1', 'field2') 1048 1049 validate(ValidationTestModelAdmin, ValidationTestModel) 1050 1046 1051 def test_save_as_validation(self): 1047 1052 1048 1053 class ValidationTestModelAdmin(ModelAdmin):