通过引用列表查询Mongoengine

时间:2013-08-26 09:08:50

标签: python mongoengine

我有2个课程(我使用的是Flask-mongoengine):

class ArticleCategory(db.Document):
    name = db.StringField(verbose_name='Title', required=True, unique=True)
    desc = db.StringField(verbose_name='Slug', required=True)

class Article(CreateUpdateMixin, DeletedMixin, db.Document):
    title = db.StringField(verbose_name='Title', required=True)
    slug = db.StringField(verbose_name='Slug', required=True, unique=True)
    content = db.StringField(verbose_name='Content', required=True)
    #Many-to-many relation
    categories = db.ListField(db.ReferenceField(ArticleCategory),
                              verbose_name='Category', required=True)

    def __unicode__(self):
        return self.title

现在我想按特定类别(或类别名称)获取所有文章。我在mongoengine查询帮助中找不到像__eq这样的运算符。

Article.objects(categories__in('some_category'))等运算符失败。

1 个答案:

答案 0 :(得分:1)

您的查询错误,请参阅文档http://mongoengine.readthedocs.io/en/latest/guide/querying.html#query-operators

Article.objects(categories__in=['ARTICLE_CATEGORY_ID'])