Ticket #5599: onetoone_relation_field_saves_v2.diff

File onetoone_relation_field_saves_v2.diff, 1.1 KB (added by Vadim Fint, 17 years ago)
  • db/models/fields/related.py

     
    126126    def __get__(self, instance, instance_type=None):
    127127        if instance is None:
    128128            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
     129        cache_name = self.related.field.get_cache_name()
     130        try:
     131            return getattr(self.related, cache_name)
     132        except AttributeError:
     133            params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
     134            rel_obj = self.related.model._default_manager.get(**params)
     135            setattr(self.related, cache_name, rel_obj)
     136            return rel_obj
    129137
    130         params = {'%s__pk' % self.related.field.name: instance._get_pk_val()}
    131         rel_obj = self.related.model._default_manager.get(**params)
    132         return rel_obj
    133 
    134138    def __set__(self, instance, value):
    135139        if instance is None:
    136140            raise AttributeError, "%s must be accessed via instance" % self.related.opts.object_name
Back to Top