1

我有Document这样EmbeddedDocumentmongoengine。怎么可以为大师创作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
4

2 回答 2

1

也必须self在 EmbeddedDocuments 中使用

class Activity(EmbeddedDocument):
    user = ReferenceField('Profile')
    user_unrelated = ReferenceField('Profile')
    created_date = DateTimeField(default=datetime.datetime.now, required=True)
于 2012-12-31T08:46:57.940 回答
0

EmbeddedDocuments应通过EmbeddedDocumentField字段类型用作 Documents 上的字段。

因此,您的 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))

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

于 2012-12-31T08:38:22.690 回答