Skip to main content
 首页 » 编程设计

python之NDB 中 SelfReferenceProperty 的等价物

2025年05月04日18myhome

查看 NDB Cheat Sheet 我发现我一直在“正确”地做,但是 使用此代码时:

class Chapter(ndb.Model): 
    parentID = ndb.KeyProperty(kind=Chapter) 

我收到这个错误

NameError: name 'Chapter' is not defined 

请您参考如下方法:

NDB 属性页面 notes:

Optional keyword argument: kind=kind, to require that keys assigned to this property always have the indicated kind. May be a string or a Model subclass.

那么用引号试试?

parentID = ndb.KeyProperty(kind='Chapter') 

在 NDB 备忘单上,它确实在引号中显示了示例,所以也许这就是实现它的方法。

ndb.KeyProperty(kind='ThisModelClass')