views:

100

answers:

2

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.

A: 

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")
Corey D
+6  A: 

That's exactly what SelfReferenceProperty is for.

Alex Martelli
Always an argument for reading the documentation. Thanks!
Ink-Jet
Ah I forgot about this. Good work :)
Corey D