Skip to content

Commit

Permalink
Fixed #22050 -- Fixed defer fields on proxy related models.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasb authored and timgraham committed Jun 25, 2014
1 parent 815e7a5 commit 9385aa3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Expand Up @@ -585,7 +585,7 @@ def deferred_to_data(self, target, callback):
must_include = {orig_opts.concrete_model: set([orig_opts.pk])}
for field_name in field_names:
parts = field_name.split(LOOKUP_SEP)
cur_model = self.model
cur_model = self.model._meta.concrete_model
opts = orig_opts
for name in parts[:-1]:
old_model = cur_model
Expand Down
5 changes: 5 additions & 0 deletions tests/defer_regress/models.py
Expand Up @@ -21,6 +21,11 @@ class RelatedItem(models.Model):
item = models.ForeignKey(Item)


class ProxyRelated(RelatedItem):
class Meta:
proxy = True


class Child(models.Model):
name = models.CharField(max_length=10)
value = models.IntegerField()
Expand Down
15 changes: 14 additions & 1 deletion tests/defer_regress/tests.py
Expand Up @@ -10,7 +10,9 @@

from .models import (
ResolveThis, Item, RelatedItem, Child, Leaf, Proxy, SimpleItem, Feature,
ItemAndSimpleItem, OneToOneItem, SpecialFeature, Location, Request)
ItemAndSimpleItem, OneToOneItem, SpecialFeature, Location, Request,
ProxyRelated,
)


class DeferRegressionTest(TestCase):
Expand Down Expand Up @@ -207,6 +209,17 @@ def test_defer_with_select_related(self):
self.assertEqual(obj.item, item2)
self.assertEqual(obj.item_id, item2.id)

def test_proxy_model_defer_with_selected_related(self):
# Regression for #22050
item = Item.objects.create(name="first", value=47)
related = RelatedItem.objects.create(item=item)
# Defer fields with only()
obj = ProxyRelated.objects.all().select_related().only('item__name')[0]
with self.assertNumQueries(0):
self.assertEqual(obj.item.name, "first")
with self.assertNumQueries(1):
self.assertEqual(obj.item.value, 47)

def test_only_with_select_related(self):
# Test for #17485.
item = SimpleItem.objects.create(name='first', value=47)
Expand Down

0 comments on commit 9385aa3

Please sign in to comment.