[英]Self-referential ReferenceProperty in Google App Engine
I'm having a bit of trouble with ReferencePropertys in App Engine (Python). 我在App Engine(Python)中使用ReferencePropertys时遇到了一些麻烦。
For a bit of fun, I'm trying to model a folder/file system, but having trouble getting folders to reference folders. 为了一点乐趣,我正在尝试建模文件夹/文件系统,但无法获取文件夹引用文件夹。
My first attempt was this: 我的第一次尝试是这样的:
class Folder(db.Model):
id = db.StringProperty()
name = db.StringProperty()
created = db.DateTimeProperty(auto_now_add=True)
folder = db.ReferenceProperty(Folder, collection_name="folders")
But that fails as "Folder" isn't defined when "folder" is trying to be defined. 但是,当尝试定义“文件夹”时,未定义“文件夹”,因此失败。
I've also tried defining "folder" outside of the main declaration for "Folder", like so: 我也尝试在“文件夹”的主声明之外定义“文件夹”,如下所示:
class Folder(db.Model):
id = db.StringProperty()
name = db.StringProperty()
created = db.DateTimeProperty(auto_now_add=True)
Folder.folder = db.ReferenceProperty(Folder, collection_name="folders")
But that fails with: AttributeError: 'Folder' object has no attribute 'folders' 但是失败了:AttributeError:'Folder'对象没有属性'folders'
I'm kind of stumped. 我有点难过。 Does anyone have experience with this, or a solution to this problem? 有没有人有这方面的经验,或解决这个问题?
Thanks in advance. 提前致谢。
这正是SelfReferenceProperty的用途。
You could create a separate model to link the two, named something like FolderChild: 你可以创建一个单独的模型来链接这两个模型,命名为FolderChild:
class FolderChild(db.Model):
parent = db.ReferenceProperty(Folder)
child = db.ReferenceProperty(Folder, collection_name="children")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.