Code

Ticket #5599: onetoone_relation_field_saves_v2.diff

File onetoone_relation_field_saves_v2.diff, 1.1 KB (added by MockSoul, 6 years ago)
Line 
1Index: db/models/fields/related.py
2===================================================================
3--- db/models/fields/related.py (revision 7003)
4+++ db/models/fields/related.py (working copy)
5@@ -126,11 +126,15 @@
6     def __get__(self, instance, instance_type=None):
7         if instance is None:
8             raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
9+        cache_name = self.related.field.get_cache_name()
10+        try:
11+            return getattr(self.related, cache_name)
12+        except AttributeError:
13+            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
14+            rel_obj = self.related.model._default_manager.get(**params)
15+            setattr(self.related, cache_name, rel_obj)
16+            return rel_obj
17
18-        params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
19-        rel_obj = self.related.model._default_manager.get(**params)
20-        return rel_obj
21-
22     def __set__(self, instance, value):
23         if instance is None:
24             raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name