0

If I have the following schema:

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

And I do the following query:

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

I get the following error:

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

This seems to only happen with allow_inheritance when certain objects may or may not have the 'author' field. If the field exists on all objects, such as the 'title' field, the query works fine.

spitz
  • 658
  • 1
  • 8
  • 19

1 Answers1

0

It seems that this is still an open issue in mongoengine that has yet to be addressed. One way around it is to use match. For example, the following does the trick:

records = Record.objects(posts__match = { 'author': author })
spitz
  • 658
  • 1
  • 8
  • 19