Code

Ticket #15494: only_pk_related.diff

File only_pk_related.diff, 846 bytes (added by sym.roe@…, 3 years ago)
Line 
1Index: django/db/models/query.py
2===================================================================
3--- django/db/models/query.py   (revision 15640)
4+++ django/db/models/query.py   (working copy)
5@@ -708,8 +708,15 @@
6             # Can only pass None to defer(), not only(), as the rest option.
7             # That won't stop people trying to do this, so let's be explicit.
8             raise TypeError("Cannot pass None as an argument to only().")
9+
10+        cleaned_fields = []
11+        for field_name in fields:
12+            if field_name == 'pk':
13+                field_name = self.model._meta.pk.name
14+            cleaned_fields.append(field_name)
15+
16         clone = self._clone()
17-        clone.query.add_immediate_loading(fields)
18+        clone.query.add_immediate_loading(cleaned_fields)
19         return clone
20 
21     def using(self, alias):