Filter_category = filters.NumberFilter(method='category_filter',label='Filter_category')
django_filters.NumberFilter类似,ModelForm中字段类型的控制
#其中method指向自己定义的过滤函数,label用于标识在测试API界面中的过滤界面字段,Filter_category控制查询字段
def category_filter(self, queryset, name,value):
print('name', name)
print('value', value)
return queryset.all()
# 这里用到多级联表查询
# return queryset.filter(Q(category_id=value)|Q(category__parent_id=value)|Q(category__parent__parent_id=value)
# |Q(category__parent__parent__parent_id=value))