Allow "pk" as a field alias in QuerySet.only() calls.
Thanks to GDorn for the patch. Fixed #15494. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16668 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
a86dde20f0
commit
7182cd2284
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue