diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index e5e11f472a1..b55de3d7665 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1791,13 +1791,18 @@ class Query(object): existing immediate values, but respects existing deferrals.) """ existing, defer = self.deferred_loading + field_names = set(field_names) + if 'pk' in field_names: + field_names.remove('pk') + field_names.add(self.model._meta.pk.name) + if defer: # Remove any existing deferred names from the current set before # setting the new names. - self.deferred_loading = set(field_names).difference(existing), False + self.deferred_loading = field_names.difference(existing), False else: # Replace any existing "immediate load" field names. - self.deferred_loading = set(field_names), False + self.deferred_loading = field_names, False def get_loaded_field_names(self): """ diff --git a/tests/modeltests/defer/tests.py b/tests/modeltests/defer/tests.py index 5f6c53dee2e..ccf19fd5ae7 100644 --- a/tests/modeltests/defer/tests.py +++ b/tests/modeltests/defer/tests.py @@ -28,11 +28,18 @@ class DeferTests(TestCase): self.assert_delayed(qs.only("name")[0], 2) self.assert_delayed(qs.defer("related__first")[0], 0) + # Using 'pk' with only() should result in 3 deferred fields, namely all + # of them except the model's primary key see #15494 + self.assert_delayed(qs.only("pk")[0], 3) + obj = qs.select_related().only("related__first")[0] self.assert_delayed(obj, 2) self.assertEqual(obj.related_id, s1.pk) + # You can use 'pk' with reverse foreign key lookups. + self.assert_delayed(s1.primary_set.all().only('pk')[0], 3) + self.assert_delayed(qs.defer("name").extra(select={"a": 1})[0], 1) self.assert_delayed(qs.extra(select={"a": 1}).defer("name")[0], 1) self.assert_delayed(qs.defer("name").defer("value")[0], 2) @@ -135,3 +142,4 @@ class DeferTests(TestCase): self.assertEqual(obj.other, "bar") obj.name = "bb" obj.save() +