简体   繁体   English

在Django,Python中使用MongoEngine查询嵌入式文档

[英]Quering Embedded Documents using MongoEngine in django, python

I need to get all EmbeddedDocuments of type Subject from Document Lesson, where Subject.subject_name = 'Something'. 我需要从Document Lesson中获取Subject类型的所有EmbeddedDocuments,其中Subject.subject_name ='Something'。 I am trying to do the following and it's not working for me: 我正在尝试执行以下操作,但对我不起作用:

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

My models: 我的模特:

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)

Thank you all in advance! 谢谢大家! Have a nice day! 祝你今天愉快! :) :)

Taking into account the following link https://mongoengine-odm.readthedocs.org/guide/querying.html it becomes obvious, that the raw function can solve the task: 考虑到以下链接https://mongoengine-odm.readthedocs.org/guide/querying.html ,很明显, 原始函数可以解决任务:

It is possible to provide a raw PyMongo query as a query parameter, which will be integrated directly into the query. 可以提供原始的PyMongo查询作为查询参数,该参数将直接集成到查询中。 This is done using the raw keyword argument: 这是使用raw关键字参数完成的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM