gpt4 book ai didi

python - Mongoengine Document对象无法使用from_json更新

转载 作者:行者123 更新时间:2023-12-01 04:53:25 25 4
gpt4 key购买 nike

来自here ,

我需要使用 from_json 更新现有文档数据。

当我使用from_json

>> user = User.objects(pk=2).first()
>> print user.to_json()

以上显示

>> {"_id": 1, "_cls": "User", "name": "OldName"}

现在我更新现有的用户对象

>> user = user.from_json(json.dumps({"_id": 1, "_cls": "User", "name": "NewName"}))
>> user.save()
>> print user.to_json()

它显示

>> {"_id": 1, "_cls": "User", "name": "NewName"}

但它无法在数据库中更新。

我再次查询它显示的相同用户

>> {"_id": 1, "_cls": "User", "name": "OldName"}

我的问题是如何使用文档对象方法 from_json 更新现有数据?

最佳答案

对我来说以下工作有效:

user.update(**mydictname) #mydictname contains the dictionary of values I want to update
user.save()
user.reload() #not doing this makes the changes available only after the object is reloaded elsewhere

关于python - Mongoengine Document对象无法使用from_json更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com