EmbeddedDocument中的ReferenceField来掌握Document

时间:2012-12-31 08:20:50

标签: python mongoengine

我在DocumentEmbeddedDocument 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}}

2 个答案:

答案 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)

应通过 EmbeddedDocumentField 字段类型将

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

注意:我正在创建一个列表,因为每个配置文件将有多个活动