0
比方说,我在peewee模型:从数据库还原peewee模型数据
inst = foo.get(id=1)
print(inst.bar) #Prints 'ValueA'
用户更改模型:
class foo(Model):
id = PrimaryKeyField()
bar = TextField(null=True)
...
我从数据库中获取的foo
实例值:
inst.bar = 'ValueB'
#inst.save() has not been called
现在用户想要将inst
还原为值e目前在数据库中。我想做到这一点:
我看到最接近的事在一个事务中包装的修改,但目前还不清楚该如何修改需要进行包装。我测试了一下,交易不工作:
with database.atomic() as txn:
inst.bar = 'ValueB'
txn.rollback()
#I also tried database.rollback() and it didn't work
print(inst.bar) #'ValueB'
包装纸调用save()
在一个事务并调用rollback()
防止被修改了数据库,但是模型实例还包含了新的价值之后。
我将如何实现我的预期行为?