Exemplo n.º 1
0
    def get(self, _obj_pk=None, **kwargs):
        if _obj_pk is not None:
            identity_map = IdentityMap(self._using)
            key = self.make_identity_key(self.model, _obj_pk)
            if identity_map.exists(key):
                return identity_map.get(key)
            try:
                obj = self.get(**{k: v for k, v in zip(to_tuple(self.pk), to_tuple(_obj_pk))})
            except ObjectDoesNotExist:
                identity_map.add(key)
                raise
            else:
                # obj added to identity_map by loader (self.load())
                return obj

        if kwargs:
            q = self.query
            for k, v in kwargs.items():
                q = q.where(self.sql_table.get_field(k) == v)
            return q[0]
Exemplo n.º 2
0
 def compute(self):
     if self._from_db:
         data_mapped = self._map_data_from_db(self._data)
     else:
         data_mapped = dict(self._data)
     identity_map = IdentityMap(self._mapper.using())
     key = self._mapper.make_identity_key(self._mapper.model, tuple(data_mapped[i] for i in to_tuple(self._mapper.pk)))
     try:
         obj = identity_map.get(key)
     except KeyError:  # First loading
         obj = self._do_load(data_mapped)
     except ObjectDoesNotExist:  # Serializable transaction level
         raise
     else:
         if reload:
             self._do_reload(obj, data_mapped)
         else:
             return obj
     self._mapper.original_data(obj, data_mapped)
     self._mapper.is_new(obj, False)
     identity_map.add(key, obj)
     return obj