Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 ramiro authored
View
5 django/db/models/sql/query.py
@@ -553,7 +553,10 @@ def deferred_to_data(self, target, callback):
columns = set()
orig_opts = self.model._meta
seen = {}
- must_include = {self.model: set([orig_opts.pk])}
+ if orig_opts.proxy:
+ must_include = {orig_opts.proxy_for_model: set([orig_opts.pk])}
+ else:
+ must_include = {self.model: set([orig_opts.pk])}
for field_name in field_names:
parts = field_name.split(LOOKUP_SEP)
cur_model = self.model
View
4 tests/regressiontests/defer_regress/models.py
@@ -34,3 +34,7 @@ def __unicode__(self):
class ResolveThis(models.Model):
num = models.FloatField()
name = models.CharField(max_length=16)
+
+class Proxy(Item):
+ class Meta:
+ proxy = True
View
19 tests/regressiontests/defer_regress/tests.py
@@ -7,7 +7,7 @@
from django.db.models.loading import cache
from django.test import TestCase
-from models import ResolveThis, Item, RelatedItem, Child, Leaf
+from models import ResolveThis, Item, RelatedItem, Child, Leaf, Proxy
class DeferRegressionTest(TestCase):
@@ -111,6 +111,7 @@ def test():
Child,
Item,
Leaf,
+ Proxy,
RelatedItem,
ResolveThis,
]
@@ -139,6 +140,7 @@ def test():
"Leaf_Deferred_name_value",
"Leaf_Deferred_second_child_value",
"Leaf_Deferred_value",
+ "Proxy",
"RelatedItem",
"RelatedItem_Deferred_",
"RelatedItem_Deferred_item_id",
@@ -146,6 +148,21 @@ def test():
]
)
+ def test_only_and_defer_usage_on_proxy_models(self):
+ # Regression for #15790 - only() broken for proxy models
+ proxy = Proxy.objects.create(name="proxy", value=42)
+
+ msg = 'QuerySet.only() return bogus results with proxy models'
+ dp = Proxy.objects.only('other_value').get(pk=proxy.pk)
+ self.assertEqual(dp.name, proxy.name, msg=msg)
+ self.assertEqual(dp.value, proxy.value, msg=msg)
+
+ # also test things with .defer()
+ msg = 'QuerySet.defer() return bogus results with proxy models'
+ dp = Proxy.objects.defer('name', 'text', 'value').get(pk=proxy.pk)
+ self.assertEqual(dp.name, proxy.name, msg=msg)
+ self.assertEqual(dp.value, proxy.value, msg=msg)
+
def test_resolve_columns(self):
rt = ResolveThis.objects.create(num=5.0, name='Foobar')
qs = ResolveThis.objects.defer('num')
Please sign in to comment.
Something went wrong with that request. Please try again.