Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #22050 -- defer fields on proxy related models #2279

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
10 changes: 9 additions & 1 deletion tests/defer_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.test import TestCase, override_settings

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


Expand Down Expand Up @@ -207,6 +207,14 @@ def test_defer_with_select_related(self):
self.assertEqual(obj.item, item2)
self.assertEqual(obj.item_id, item2.id)

related = RelatedItem.objects.create(item=item1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason not to use a separate test method?

### 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