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


Python说:

 NameError: name 'Profile' is not defined

最佳答案

必须在EmbeddedDocuments中也使用self

class Activity(EmbeddedDocument):
    user = ReferenceField('Profile')
    user_unrelated = ReferenceField('Profile')
    created_date = DateTimeField(default=datetime.datetime.now, required=True)

关于python - EmbeddedDocument中的ReferenceField用来控制文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14099249/

10-09 03:04