Django Save方法需要两次更新模型实例才能工作。
应用程序具有以下保存方法,可根据ID生成唯一ID。
def save(self, *args, **kwargs):
if self.id and not self.fid:
self.fid = encode(self.id)
实际上它只适用于模型实例保存两次,第一个FID为无。
答案 0 :(得分:1)
def save(self, *args, **kwargs):
# save the instance, do this first so that we have an id
super(MyModelClass, self).save(*args, **kwargs)
# if it was a new instance fid is not set yet
if self.id and not self.fid:
self.fid = encode(self.id)
# save the instance again now that we've set fid
super(MyModelClass, self).save(*args, **kwargs)