定制QuerySets mongoengine中的过滤器问题

时间:2019-07-10 06:58:03

标签: python python-3.x mongoengine

我有一个MongoEngine模型Place

class Place(Document):
    is_favorite = fields.BooleanField(default=False)
    name = fields.StringField(max_length=150)
    price_tier = fields.IntField(min_value=1, max_value=3)

    meta = {
    'queryset_class': AwesomerQuerySet
    }

我正在使用MongoEngine中的Custom QuerySets


class AwesomerQuerySet(QuerySet):
    def getNinePlaces1(self):
        #the below line returns empty queryset
        qSet = self.filter(price_tier=2)[:9]
        return qSet

    def getNinePlaces2(self):
        #the below line returns proper queryset
        qSet = self.filter(is_favorite=True)[:9]
        return qSet

输出:

print(Place.objects.getNinePlaces1())
>> []

print(Place.objects.getNinePlaces2())
>> [<Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>, <Place: Place object>]

问题: 我不确定为什么Place.objects.getNin​​ePlaces1()返回空集。请帮助我理解。

让我知道我是否不清楚。谢谢!

0 个答案:

没有答案