[4.2.x] Fixed #34458 -- Fixed QuerySet.defer() crash on attribute names.

Thanks Andrew Cordery for the report.

Regression in b3db6c8dcb.

Backport of 87c63bd8df from main
This commit is contained in:
Simon Charette 2023-04-04 16:58:15 -04:00 committed by Mariusz Felisiak
parent 272dc386c2
commit 647920b766
3 changed files with 9 additions and 2 deletions

View File

@ -702,7 +702,8 @@ class Query(BaseExpression):
# by recursively caling this function. # by recursively caling this function.
for field in opts.concrete_fields: for field in opts.concrete_fields:
field_mask = mask.pop(field.name, None) field_mask = mask.pop(field.name, None)
if field_mask is None: field_att_mask = mask.pop(field.attname, None)
if field_mask is None and field_att_mask is None:
select_mask.setdefault(field, {}) select_mask.setdefault(field, {})
elif field_mask: elif field_mask:
if not field.is_relation: if not field.is_relation:

View File

@ -9,4 +9,5 @@ Django 4.2.1 fixes several bugs in 4.2.
Bugfixes Bugfixes
======== ========
* ... * Fixed a regression in Django 4.2 that caused a crash of ``QuerySet.defer()``
when deferring fields by attribute names (:ticket:`34458`).

View File

@ -178,6 +178,11 @@ class DeferTests(AssertionMixin, TestCase):
obj = ShadowChild.objects.defer("name").get() obj = ShadowChild.objects.defer("name").get()
self.assertEqual(obj.name, "adonis") self.assertEqual(obj.name, "adonis")
def test_defer_fk_attname(self):
primary = Primary.objects.defer("related_id").get()
with self.assertNumQueries(1):
self.assertEqual(primary.related_id, self.p1.related_id)
class BigChildDeferTests(AssertionMixin, TestCase): class BigChildDeferTests(AssertionMixin, TestCase):
@classmethod @classmethod