【发布时间】:2021-04-08 16:13:41
【问题描述】:
我想显示原始数据和修改后的数据之间的差异,在尝试了很多方法后,我们决定将更新后的数据和更新前的数据插入到名为history_table 的表中,并使用 JSON。
如何将更改的字典和更新的字典插入下表?
# table of `history_table`
before_updated updated updated_time
json of before_updated json of updated datetime
下面的代码将更新来自不同表的 15 行(示例代码只有一个表),所以需要更改和保存很多表,我被困了很多天,非常感谢任何建议。
class GetTestDetail(Resource):
args = self.test.parse_args()
print(args)
id = args.get("id")
def get(self):
# get something
data = db.session.query(Test.name, Test.days).filter(Test.id == id).first()
return dict(data=data)
def put(self):
# change something
args = self.test.parse_args()
test = args.get("test")
dict_test = db.session.query(Test).filter_by(id=id).first()
test["name"] = 'Alice'
test["days"] = 12
{setattr(dict_test, k, v) for k, v in test.items()}
db.session.commit()
return {"status": True, "message": "Successfully"}
except Exception as e:
db.session.rollback()
return {"status": False, "message": "Failed"}
【问题讨论】:
标签: python json flask flask-sqlalchemy