Loading django/db/models/sql/compiler.py +2 −3 Original line number Diff line number Diff line Loading @@ -713,9 +713,8 @@ class SQLCompiler(object): # into `resolve_columns` because it wasn't selected. only_load = self.deferred_to_columns() if only_load: db_table = self.query.get_meta().db_table fields = [f for f in fields if db_table in only_load and f.column in only_load[db_table]] fields = [f for f in fields if f.model._meta.db_table not in only_load or f.column in only_load[f.model._meta.db_table]] if has_aggregate_select: # pad None in to fields for aggregates fields = fields[:aggregate_start] + [ Loading tests/queries/models.py +9 −0 Original line number Diff line number Diff line Loading @@ -535,3 +535,12 @@ class StaffUser(BaseUser): def __str__(self): return self.staff class Ticket21203Parent(models.Model): parentid = models.AutoField(primary_key=True) parent_bool = models.BooleanField(default=True) created = models.DateTimeField(auto_now=True) class Ticket21203Child(models.Model): childid = models.AutoField(primary_key=True) parent = models.ForeignKey(Ticket21203Parent) tests/queries/tests.py +9 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ from .models import ( ModelA, ModelB, ModelC, ModelD, Responsibility, Job, JobResponsibilities, BaseA, FK1, Identifier, Program, Channel, Page, Paragraph, Chapter, Book, MyObject, Order, OrderItem, SharedConnection, Task, Staff, StaffUser, CategoryRelationship) CategoryRelationship, Ticket21203Parent, Ticket21203Child) class BaseQuerysetTest(TestCase): def assertValueQuerysetEqual(self, qs, values): Loading Loading @@ -3071,3 +3071,11 @@ class Ticket20955Tests(TestCase): task_get.creator.staffuser.staff) self.assertEqual(task_select_related.owner.staffuser.staff, task_get.owner.staffuser.staff) class Ticket21203Tests(TestCase): def test_ticket_21203(self): p = Ticket21203Parent.objects.create(parent_bool=True) c = Ticket21203Child.objects.create(parent=p) qs = Ticket21203Child.objects.select_related('parent').defer('parent__created') self.assertQuerysetEqual(qs, [c], lambda x: x) self.assertIs(qs[0].parent.parent_bool, True) Loading
django/db/models/sql/compiler.py +2 −3 Original line number Diff line number Diff line Loading @@ -713,9 +713,8 @@ class SQLCompiler(object): # into `resolve_columns` because it wasn't selected. only_load = self.deferred_to_columns() if only_load: db_table = self.query.get_meta().db_table fields = [f for f in fields if db_table in only_load and f.column in only_load[db_table]] fields = [f for f in fields if f.model._meta.db_table not in only_load or f.column in only_load[f.model._meta.db_table]] if has_aggregate_select: # pad None in to fields for aggregates fields = fields[:aggregate_start] + [ Loading
tests/queries/models.py +9 −0 Original line number Diff line number Diff line Loading @@ -535,3 +535,12 @@ class StaffUser(BaseUser): def __str__(self): return self.staff class Ticket21203Parent(models.Model): parentid = models.AutoField(primary_key=True) parent_bool = models.BooleanField(default=True) created = models.DateTimeField(auto_now=True) class Ticket21203Child(models.Model): childid = models.AutoField(primary_key=True) parent = models.ForeignKey(Ticket21203Parent)
tests/queries/tests.py +9 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,7 @@ from .models import ( ModelA, ModelB, ModelC, ModelD, Responsibility, Job, JobResponsibilities, BaseA, FK1, Identifier, Program, Channel, Page, Paragraph, Chapter, Book, MyObject, Order, OrderItem, SharedConnection, Task, Staff, StaffUser, CategoryRelationship) CategoryRelationship, Ticket21203Parent, Ticket21203Child) class BaseQuerysetTest(TestCase): def assertValueQuerysetEqual(self, qs, values): Loading Loading @@ -3071,3 +3071,11 @@ class Ticket20955Tests(TestCase): task_get.creator.staffuser.staff) self.assertEqual(task_select_related.owner.staffuser.staff, task_get.owner.staffuser.staff) class Ticket21203Tests(TestCase): def test_ticket_21203(self): p = Ticket21203Parent.objects.create(parent_bool=True) c = Ticket21203Child.objects.create(parent=p) qs = Ticket21203Child.objects.select_related('parent').defer('parent__created') self.assertQuerysetEqual(qs, [c], lambda x: x) self.assertIs(qs[0].parent.parent_bool, True)