简体   繁体   中英

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.

这正是SelfReferenceProperty的用途。

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM