如何在 python 上使用 peewee ORM 从 JSON 加载的对象更新 table

how to update a table from a JSON loaded object using peewee ORM on python

from schema import db, CustomerTable as GF
db.connect()

#this works
query = GF.update(Priority=88).where(GF.CustomerID==1)
query.execute()

有人可以帮我解决这个问题吗?上面的代码可以很好地更新 sqlite 数据库。底部没有。

#this doesn't work and help needed...

#JSON string
customers = '{ "name":"john john", "mobile":12345678, "email":"john.doe@gmail.com"}'
pycustomers = json.loads(customers)

# print(pycustomers["name"])
for keys in pycustomers:
    print("This is the key: ", keys, "\t\tThis is the value: ", pycustomers[keys])
    query = GF.update({keys: pycustomers[keys]}).where(GF.Mobile==pycustomers["mobile"])

如果JSON字典的所有键都对应模型上的字段,你可以直接将数据传入:

customers = '{ "name":"john john", "mobile":12345678, "email":"john.doe@gmail.com"}'
pycustomers = json.loads(customers)

# print(pycustomers["name"])
nrows = (GF.update(pycustomers)
        .where(GF.Mobile==pycustomers["mobile"])
        .execute())

可能有用的文档: