Ticket #15494: only_pk.diff
File only_pk.diff, 2.2 KB (added by , 14 years ago) |
---|
-
django/db/models/sql/query.py
1738 1738 existing immediate values, but respects existing deferrals.) 1739 1739 """ 1740 1740 existing, defer = self.deferred_loading 1741 field_names = set(field_names) 1742 if 'pk' in field_names: 1743 field_names.remove('pk') 1744 field_names.add(self.model._meta.pk.name) 1745 1741 1746 if defer: 1742 1747 # Remove any existing deferred names from the current set before 1743 1748 # setting the new names. 1744 self.deferred_loading = set(field_names).difference(existing), False1749 self.deferred_loading = field_names.difference(existing), False 1745 1750 else: 1746 1751 # Replace any existing "immediate load" field names. 1747 self.deferred_loading = set(field_names), False1752 self.deferred_loading = field_names, False 1748 1753 1749 1754 def get_loaded_field_names(self): 1750 1755 """ -
tests/modeltests/defer/tests.py
27 27 self.assert_delayed(qs.defer("name")[0], 1) 28 28 self.assert_delayed(qs.only("name")[0], 2) 29 29 self.assert_delayed(qs.defer("related__first")[0], 0) 30 31 #Using 'pk' with only() should result in 3 deferred fields, 32 #namely all of them except the model's primary key 33 #see #15494 34 self.assert_delayed(qs.only("pk")[0], 3) 30 35 31 36 obj = qs.select_related().only("related__first")[0] 32 37 self.assert_delayed(obj, 2) 33 38 34 39 self.assertEqual(obj.related_id, s1.pk) 40 41 #You can use 'pk' with reverse foreign key lookups 42 self.assert_delayed(s1.primary_set.all().only('pk')[0], 3) 35 43 36 44 self.assert_delayed(qs.defer("name").extra(select={"a": 1})[0], 1) 37 45 self.assert_delayed(qs.extra(select={"a": 1}).defer("name")[0], 1) … … 135 143 self.assertEqual(obj.other, "bar") 136 144 obj.name = "bb" 137 145 obj.save() 146 147 148 149 No newline at end of file