From 5074c75a37f88726f3ae057999144545881d3cfc Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 30 May 2013 11:05:42 -0400 Subject: [PATCH] Fixed #16856 - Added a way to clear select_related. Thanks Carl for the suggestion and David Cramer for the patch. --- django/db/models/query.py | 6 +++++- docs/ref/models/querysets.txt | 7 +++++++ docs/releases/1.6.txt | 5 +++++ tests/select_related/tests.py | 4 ++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 5e8b3d995c7..b0ce25f5b50 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -645,6 +645,8 @@ class QuerySet(object): If fields are specified, they must be ForeignKey fields and only those related objects are included in the selection. + + If select_related(None) is called, the list is cleared. """ if 'depth' in kwargs: warnings.warn('The "depth" keyword argument has been deprecated.\n' @@ -654,7 +656,9 @@ class QuerySet(object): raise TypeError('Unexpected keyword arguments to select_related: %s' % (list(kwargs),)) obj = self._clone() - if fields: + if fields == (None,): + obj.query.select_related = False + elif fields: if depth: raise TypeError('Cannot pass both "depth" and fields to select_related()') obj.query.add_select_related(fields) diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 3fb5b3a4985..2788143899a 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -774,6 +774,13 @@ You can also refer to the reverse direction of a is defined. Instead of specifying the field name, use the :attr:`related_name ` for the field on the related object. +.. versionadded:: 1.6 + +If you need to clear the list of related fields added by past calls of +``select_related`` on a ``QuerySet``, you can pass ``None`` as a parameter:: + + >>> without_relations = queryset.select_related(None) + .. deprecated:: 1.5 The ``depth`` parameter to ``select_related()`` has been deprecated. You should replace it with the use of the ``(*fields)`` listing specific diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index efc1297265a..c8e6044a06d 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -287,6 +287,11 @@ Minor features helper for testing formset errors: :meth:`~django.test.SimpleTestCase.assertFormsetError`. +* The list of related fields added to a + :class:`~django.db.models.query.QuerySet` by + :meth:`~django.db.models.query.QuerySet.select_related` can be cleared using + ``select_related(None)``. + Backwards incompatible changes in 1.6 ===================================== diff --git a/tests/select_related/tests.py b/tests/select_related/tests.py index 27d65fecb1e..baa141d123e 100644 --- a/tests/select_related/tests.py +++ b/tests/select_related/tests.py @@ -172,3 +172,7 @@ class SelectRelatedTests(TestCase): Species.objects.select_related, 'genus__family__order', depth=4 ) + + def test_none_clears_list(self): + queryset = Species.objects.select_related('genus').select_related(None) + self.assertEqual(queryset.query.select_related, False)