如何使用allow_inheritance

时间:2017-03-06 16:51:50

标签: mongodb mongoengine flask-mongoengine

如果我有以下架构:

class Post(EmbeddedDocument):
    title = StringField(max_length=120, required=True)
    meta = {'allow_inheritance': True}

class TextPost(Post):
    content = StringField()

class MoviePost(Post):
    author = ReferenceField(Authors)

class Record(Document):
    posts = ListField(EmbeddedDocumentField(Post))

我做了以下查询:

author = Author.objects.get_or_404(id = id)
records = Record.objects(posts__author = author)
records.count()

我收到以下错误:

AttributeError: 'author' object has no attribute 'get'

这似乎只发生在allow_inheritance时,某些对象可能有也可能没有'author'字段。如果该字段存在于所有对象上,例如“标题”字段,则查询工作正常。

1 个答案:

答案 0 :(得分:0)

似乎这仍然是mongoengine中尚未解决的问题。解决它的一种方法是使用match。例如,以下是诀窍:

records = Record.objects(posts__match = { 'author': author })
相关问题