我正在使用
https://github.com/alex/django-filter/的django-rest-framework,但问题主要是关于django-filter.我无法理解如何使用“__in”查找过滤器.
例如,我有模型:
class Book(models.Model): name = models.CharField(max_length=100) class BookView(viewsets.ReadOnlyModelViewSet): serializer_class = BookSerializer() model = Book filter_fields = ('id','name')
我不能像这样使用网址
/v1/books/?id__in=1,2,3
找到id为1,2或3的书籍
解决方法
本期讨论的问题是:
https://github.com/alex/django-filter/issues/137#issuecomment-77697870
from django_filters import Filter from django_filters.fields import Lookup from .models import Product class ListFilter(Filter): def filter(self,qs,value): value_list = value.split(u',') return super(ListFilter,self).filter(qs,Lookup(value_list,'in')) class ProductFilterSet(django_filters.FilterSet): id = ListFilter(name='id') class Meta: model = Product fields = ['id']
你可以写下面的内容:
products/?id=7,8,9