开发者

Self-referential ReferenceProperty in Google App Engine

I'm having a bit of trouble with ReferencePropertys in App Engine (Python).

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'

I'm kind of stumped. Does anyone have experience with this, or a solution to this problem?

开发者_如何学编程

Thanks in advance.


That's exactly what SelfReferenceProperty is for.


You could create a separate model to link the two, named something like FolderChild:

class FolderChild(db.Model):
    parent = db.ReferenceProperty(Folder)
    child = db.ReferenceProperty(Folder, collection_name="children")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜