Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16856 - Added a way to clear select_related.

Thanks Carl for the suggestion and David Cramer for the patch.
  • Loading branch information...
commit 5074c75a37f88726f3ae057999144545881d3cfc 1 parent 616f3c4
Tim Graham authored May 30, 2013
6  django/db/models/query.py
@@ -645,6 +645,8 @@ def select_related(self, *fields, **kwargs):
645 645
 
646 646
         If fields are specified, they must be ForeignKey fields and only those
647 647
         related objects are included in the selection.
  648
+
  649
+        If select_related(None) is called, the list is cleared.
648 650
         """
649 651
         if 'depth' in kwargs:
650 652
             warnings.warn('The "depth" keyword argument has been deprecated.\n'
@@ -654,7 +656,9 @@ def select_related(self, *fields, **kwargs):
654 656
             raise TypeError('Unexpected keyword arguments to select_related: %s'
655 657
                     % (list(kwargs),))
656 658
         obj = self._clone()
657  
-        if fields:
  659
+        if fields == (None,):
  660
+            obj.query.select_related = False
  661
+        elif fields:
658 662
             if depth:
659 663
                 raise TypeError('Cannot pass both "depth" and fields to select_related()')
660 664
             obj.query.add_select_related(fields)
7  docs/ref/models/querysets.txt
@@ -774,6 +774,13 @@ You can also refer to the reverse direction of a
774 774
 is defined. Instead of specifying the field name, use the :attr:`related_name
775 775
 <django.db.models.ForeignKey.related_name>` for the field on the related object.
776 776
 
  777
+.. versionadded:: 1.6
  778
+
  779
+If you need to clear the list of related fields added by past calls of
  780
+``select_related`` on a ``QuerySet``, you can pass ``None`` as a parameter::
  781
+
  782
+   >>> without_relations = queryset.select_related(None)
  783
+
777 784
 .. deprecated:: 1.5
778 785
     The ``depth`` parameter to ``select_related()`` has been deprecated. You
779 786
     should replace it with the use of the ``(*fields)`` listing specific
5  docs/releases/1.6.txt
@@ -287,6 +287,11 @@ Minor features
287 287
   helper for testing formset errors:
288 288
   :meth:`~django.test.SimpleTestCase.assertFormsetError`.
289 289
 
  290
+* The list of related fields added to a
  291
+  :class:`~django.db.models.query.QuerySet` by
  292
+  :meth:`~django.db.models.query.QuerySet.select_related` can be cleared using
  293
+  ``select_related(None)``.
  294
+
290 295
 Backwards incompatible changes in 1.6
291 296
 =====================================
292 297
 
4  tests/select_related/tests.py
@@ -172,3 +172,7 @@ def test_depth_fields_fails(self):

0 notes on commit 5074c75

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