在django,python中使用MongoEngine查询嵌入式文档

时间:2015-05-22 15:46:23

标签: python django mongoengine

我需要从Document Lesson获取所有类型为Subject的EmbeddedDocuments,其中Subject.subject_name =' Something'。 我正在努力做以下事情并且它不适合我:

class Subject(EmbeddedDocument):
    subject_code = IntField()
    subject_name = StringField(max_length=60)
    hours = IntField(required=False)

    def __unicode__(self):
        return smart_unicode(self.subject_name)

class Lesson(Document):
    subject = EmbeddedDocumentField(Subject)
    teacher = EmbeddedDocumentField(Teacher)
    group = EmbeddedDocumentField(Group)
    room = EmbeddedDocumentField(Room)
    attendance = IntField()

    def __unicode__(self):
        return smart_unicode(self.id)

我的模特:

{{1}}

提前谢谢大家! 祝你今天愉快! :)

1 个答案:

答案 0 :(得分:0)

考虑到以下链接https://mongoengine-odm.readthedocs.org/guide/querying.html,很明显,原始功能可以解决任务:

可以提供原始PyMongo查询作为查询参数,该查询将直接集成到查询中。这是使用原始关键字参数完成的:

results = Lesson.objects(__raw__={'subject.subject_name': 'Math'})