gpt4 book ai didi

google-app-engine - 使用 ndb.KeyProperty 如何引用相同的模型?

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:45 26 4
gpt4 key购买 nike

我有一个简单的场景,其中有一个 User 类,它具有 name、email 和 followers 属性。

class User(ndb.Model):
name = ndb.StringProperty(required = True)
search_name = ndb.StringProperty(required = True)
pw_hash = ndb.StringProperty(required = True)
email = ndb.StringProperty()

follows = ndb.KeyProperty(Follow, repeated=True)
followers = ndb.KeyProperty(User, repeated=True)

执行此操作时出现错误。

File "C:\ujjal\my project\cravel\code2\Users.py", line 46, in User
followers = ndb.KeyProperty(User, repeated=True)
NameError: name 'User' is not defined
INFO 2012-09-11 11:45:23,953 dev_appserver.py:2967] "GET / HTTP/1.1" 500 -

任何关于如何为“followers”属性建模的建议都将受到高度赞赏。提前致谢

最佳答案

在关键属性上使用种类 例如some_prop = ndb.KeyProperty(User) 只强制 key 的类型必须是用户类型。因此,如果需要,您仍然可以使用没有种类的 KeyProperty。

但是,如果你想强制所有跟随键必须是用户类型(在 User 模型中),那么用引号将类型括起来:

followers = ndb.KeyProperty(kind='User', repeated=True)

ndb cheat sheet 中解释得更好一些

关于google-app-engine - 使用 ndb.KeyProperty 如何引用相同的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12369014/

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