Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15790 -- Fixed QuerySet only() and defer() methods behavior wi…

…th proxy models. Thanks Michal Modzelewzki for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16228 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 07bfc76ecf22bfcd0cdbabe6af8639db2a0253dd 1 parent c5cb2fa
Ramiro Morales authored May 15, 2011
5  django/db/models/sql/query.py
@@ -553,7 +553,10 @@ def deferred_to_data(self, target, callback):
553 553
         columns = set()
554 554
         orig_opts = self.model._meta
555 555
         seen = {}
556  
-        must_include = {self.model: set([orig_opts.pk])}
  556
+        if orig_opts.proxy:
  557
+            must_include = {orig_opts.proxy_for_model: set([orig_opts.pk])}
  558
+        else:
  559
+            must_include = {self.model: set([orig_opts.pk])}
557 560
         for field_name in field_names:
558 561
             parts = field_name.split(LOOKUP_SEP)
559 562
             cur_model = self.model
4  tests/regressiontests/defer_regress/models.py
@@ -34,3 +34,7 @@ def __unicode__(self):
34 34
 class ResolveThis(models.Model):
35 35
     num = models.FloatField()
36 36
     name = models.CharField(max_length=16)
  37
+
  38
+class Proxy(Item):
  39
+    class Meta:
  40
+        proxy = True
19  tests/regressiontests/defer_regress/tests.py
@@ -7,7 +7,7 @@
7 7
 from django.db.models.loading import cache
8 8
 from django.test import TestCase
9 9
 
10  
-from models import ResolveThis, Item, RelatedItem, Child, Leaf
  10
+from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy
11 11
 
12 12
 
13 13
 class DeferRegressionTest(TestCase):
@@ -111,6 +111,7 @@ def test():
111 111
                 Child,
112 112
                 Item,
113 113
                 Leaf,
  114
+                Proxy,
114 115
                 RelatedItem,
115 116
                 ResolveThis,
116 117
             ]
@@ -139,6 +140,7 @@ def test():
139 140
                 "Leaf_Deferred_name_value",
140 141
                 "Leaf_Deferred_second_child_value",
141 142
                 "Leaf_Deferred_value",
  143
+                "Proxy",
142 144
                 "RelatedItem",
143 145
                 "RelatedItem_Deferred_",
144 146
                 "RelatedItem_Deferred_item_id",
@@ -146,6 +148,21 @@ def test():
146 148
             ]
147 149
         )
148 150
 
  151
+    def test_only_and_defer_usage_on_proxy_models(self):
  152
+        # Regression for #15790 - only() broken for proxy models
  153
+        proxy = Proxy.objects.create(name="proxy", value=42)
  154
+
  155
+        msg = 'QuerySet.only() return bogus results with proxy models'
  156
+        dp = Proxy.objects.only('other_value').get(pk=proxy.pk)
  157
+        self.assertEqual(dp.name, proxy.name, msg=msg)
  158
+        self.assertEqual(dp.value, proxy.value, msg=msg)
  159
+
  160
+        # also test things with .defer()
  161
+        msg = 'QuerySet.defer() return bogus results with proxy models'
  162
+        dp = Proxy.objects.defer('name', 'text', 'value').get(pk=proxy.pk)
  163
+        self.assertEqual(dp.name, proxy.name, msg=msg)
  164
+        self.assertEqual(dp.value, proxy.value, msg=msg)
  165
+
149 166
     def test_resolve_columns(self):
150 167
         rt = ResolveThis.objects.create(num=5.0, name='Foobar')
151 168
         qs = ResolveThis.objects.defer('num')

0 notes on commit 07bfc76

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