Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13227 -- Ensure that the query cache is flushed when a QuerySe…

…t is deepcopied, avoiding problems when an evaluated queryset is used as a subquery. Thanks to claudep for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12970 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e93f56b174b6105943b68dfc3d2b81ccdc3c364a 1 parent b3390fe
Russell Keith-Magee authored April 13, 2010
9  django/db/models/query.py
@@ -45,11 +45,12 @@ def __deepcopy__(self, memo):
45 45
         """
46 46
         Deep copy of a QuerySet doesn't populate the cache
47 47
         """
48  
-        obj_dict = deepcopy(self.__dict__, memo)
49  
-        obj_dict['_iter'] = None
50  
-
51 48
         obj = self.__class__()
52  
-        obj.__dict__.update(obj_dict)
  49
+        for k,v in self.__dict__.items():
  50
+            if k in ('_iter','_result_cache'):
  51
+                obj.__dict__[k] = None
  52
+            else:
  53
+                obj.__dict__[k] = deepcopy(v, memo)
53 54
         return obj
54 55
 
55 56
     def __getstate__(self):
20  tests/regressiontests/queries/tests.py
@@ -4,7 +4,7 @@
4 4
 from django.db.models import Count
5 5
 from django.test import TestCase
6 6
 
7  
-from models import Tag, Annotation, DumbCategory
  7
+from models import Tag, Annotation, DumbCategory, Note, ExtraInfo
8 8
 
9 9
 class QuerysetOrderedTests(unittest.TestCase):
10 10
     """
@@ -63,3 +63,21 @@ def test_sliced_delete(self):
63 63
             # This prevents us from even evaluating this test case at all.
64 64
             # Refs #10099
65 65
             self.assertFalse(connections[DEFAULT_DB_ALIAS].features.allow_sliced_subqueries)
  66
+
  67
+class CloneTests(TestCase):
  68
+    def test_evaluated_queryset_as_argument(self):
  69
+        "#13227 -- If a queryset is already evaluated, it can still be used as a query arg"
  70
+        n = Note(note='Test1', misc='misc')
  71
+        n.save()
  72
+        e = ExtraInfo(info='good', note=n)
  73
+        e.save()
  74
+
  75
+        n_list = Note.objects.all()
  76
+        # Evaluate the Note queryset, populating the query cache
  77
+        list(n_list)
  78
+        # Use the note queryset in a query, and evalute
  79
+        # that query in a way that involves cloning.
  80
+        try:
  81
+            self.assertEquals(ExtraInfo.objects.filter(note__in=n_list)[0].info, 'good')
  82
+        except:
  83
+            self.fail('Query should be clonable')

0 notes on commit e93f56b

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