def save(self):
''' Persists this object to the database. Each field knows how to store
itself so we don't have to worry about it '''
redis = type(self).get_redis()
pipe = to_pipeline(redis)
pipe.hset(self.key(), 'id', self.id)
for fieldname, field in self.proxy:
if not isinstance(field, Relation):
field.save(getattr(self, fieldname), pipe, commit=False)
pipe.sadd(type(self).members_key(), self.id)
pipe.execute()
if self.notify:
data = json.dumps({
'event': 'create' if not self._persisted else 'update',
'data': self.to_json(),
})
redis.publish(type(self).cls_key(), data)
redis.publish(self.key(), data)
self._persisted = True
return self
评论列表
文章目录