gpt4 book ai didi

python - Python Google App Engine 中隐式 dict 到 ndb.Model 转换

转载 作者:行者123 更新时间:2023-11-30 22:46:57 27 4
gpt4 key购买 nike

我遇到了这个功能(?),其中字典隐式转换为 ndb.Model 对象

我有以下ndb.Model类

class DateOfBirth(ndb.Model)
day = ndb.IntegerProperty()
month = ndb.IntegerProperty()
year = ndb.IntegerProperty()

class User(ndb.Model):
dob = ndb.StructuredProperty(DateofBirth)

在一个地方,当我不小心传入一个字典时

user.dob = {"日": 12, "月": 10, "年": 1983}

它没有提示,而且看起来很有效。

这是预期的吗,还是我预计以后会遇到问题(因为这种行为没有记录并且预计会随时中断)

最佳答案

这对我来说是一个惊喜,我已经使用NDB很长时间了!但从代码来看,似乎是有意为之:https://github.com/GoogleCloudPlatform/datastore-ndb-python/blob/caac0c3e7dd4d9b2c6b32dfc5d59386dd02e6b57/ndb/model.py#L2354

只需对代码进行很小的更改即可不必依赖该行为:

user.dob = DateOfBirth(**{"day": 12, "month": 10, "year": 1983})

关于python - Python Google App Engine 中隐式 dict 到 ndb.Model 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40690083/

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