我在Document
中EmbeddedDocument
mongoengine
这样ReferenceField
。
如何在EmbeddedDocument
中为主人Document
创建Document
...主人EmbeddedDocument
表示嵌入我的class Activity(EmbeddedDocument):
user = ReferenceField(Profile)
followed_user = ReferenceField(Profile)
created_date = DateTimeField(default=datetime.datetime.now, required=True)
class Profile(Document):
username = StringField(max_length=255, required=True)
password = StringField(max_length=255, required=True)
email = StringField(max_length=255, required=True)
activities = ListField(EmbeddedDocumentField(Activity))
NameError: name 'Profile' is not defined
Python说:
{{1}}
答案 0 :(得分:1)
也必须在EmbeddedDocuments中使用self
class Activity(EmbeddedDocument):
user = ReferenceField('Profile')
user_unrelated = ReferenceField('Profile')
created_date = DateTimeField(default=datetime.datetime.now, required=True)
答案 1 :(得分:0)
EmbeddedDocuments 用作文档上的字段。
因此,您的Profile类应该像
class Profile(Document):
username = StringField(max_length=255, required=True)
password = StringField(max_length=255, required=True)
email = StringField(max_length=255, required=True)
activities = ListField(EmbeddedDocumentField(Activity))
注意:我正在创建一个列表,因为每个配置文件将有多个活动