django-filter 实现过滤时查询是否包含在数组的方法,in数组的实现

查了半天无解,还是在官网找到的,记录一下

使用

BaseInFilter

官网地址:https://django-filter.readthedocs.io/en/master/ref/filters.html#baseinfilter

以下实现代码来自于官网

class NumberInFilter(BaseInFilter, NumberFilter):
    pass

class F(FilterSet):
    id__in = NumberInFilter(field_name='id', lookup_expr='in')

    class Meta:
        model = User

User.objects.create(username='alex')
User.objects.create(username='jacob')
User.objects.create(username='aaron')
User.objects.create(username='carl')

# In: User with IDs 1 and 3.
f = F({'id__in': '1,3'})
assert len(f.qs) == 2

 

 
posted @ 2019-08-07 16:45  地表最强队队员  阅读(2885)  评论(0编辑  收藏  举报