django-filter 自定义
作者:互联网
在使用django-filter的时候,可能需要对一个字段进行多个值筛选,比如&fltr_city=杭州,温州,这个时候可以使用自定义的过滤器。
当有多个筛选条件,自定义的过滤器一定要返回queryset的过滤值(queryset是其他筛选的返回值)
点击查看代码
class CustomerFilter(filters.FilterSet):
flter_region = filters.CharFilter(field_name='unit__city__province__region_region_name', lookup_expr='icontains', ,help_text='筛选的大区')
fltr_city = filters.CharFilter(method='filter_city', lookup_expr='in')
class Meta:
model = TbCustomer
fields = ['flter_region', 'fltr_city']
def filter_city(self, queryset, name, value):
value_list = list(filter(None, value.split(',')))
city_list = TbUnit.objects.filter(status, city__city_name__in=value_list)
city_id = [i.id for i in city_list]
d = TbCustomer.objects.filter(status=0,unit_id__in=city_id).all()
c_id = [i.id for i in d]
return queryset.filter(id__in=c_id)
标签:__,city,自定义,queryset,list,django,filter,id 来源: https://www.cnblogs.com/bkylww/p/16054306.html