Implementing Bi-Directional relationships in MongoEngine Implementing Bi-Directional relationships in MongoEngine mongodb mongodb

Implementing Bi-Directional relationships in MongoEngine


This is the proper solution:

from mongoengine import *class User(Document):    name = StringField()    page = ReferenceField('Page')class Page(Document):    content = StringField()    user = ReferenceField(User)

Use single quotes ('Page') to denote classes that have not yet been defined.


Drew's answer is the best way in this case, but I wanted to mention that you can also use a GenereicReferenceField:

from mongoengine import *class User(Document):    name = StringField()    page = GenericReferenceField()class Page(Document):    content = StringField()    user = ReferenceField(User)

But again, for your specific problem, go with the class name in single quotes.