Code

Ticket #5599: onetoone_relation_field_saves.diff

File onetoone_relation_field_saves.diff, 868 bytes (added by MockSoul, 7 years ago)

Fast fixup.

Line 
1--- db/models/fields/related.py (revision 7003)
2+++ db/models/fields/related.py (working copy)
3@@ -126,10 +126,14 @@
4     def __get__(self, instance, instance_type=None):
5         if instance is None:
6             raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
7+        try:
8+            self.__cached_rel_obj
9+        except AttributeError:
10+            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
11+            rel_obj = self.related.model._default_manager.get(**params)
12+            self.__cached_rel_obj = rel_obj
13
14-        params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
15-        rel_obj = self.related.model._default_manager.get(**params)
16-        return rel_obj
17+        return self.__cached_rel_obj
18
19     def __set__(self, instance, value):
20         if instance is None: