过滤组件
基于django内置的过滤组
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import ListAPIView
需继承generics
class BooksGenericListApiView(ListAPIView): authentication_classes = [] queryset = models.Book.objects.all() serializer_class = BookModelSerializer filter_backends = [DjangoFilterBackend]
定义过滤器
filterset_fields=['title','addr']
定义过滤方式和字段
路由
http://127.0.0.1:8000/b2oks/?price=46&addr=建德
排序组件
这个需要借用django_filter pip install 即可
from django_filters.rest_framework import DjangoFilterBackend from rest_framework.filters import OrderingFilter from rest_framework.generics import ListAPIView class BooksGenericListApiView(ListAPIView): authentication_classes = [] queryset = models.Book.objects.all() serializer_class = BookModelSerializer filter_backends = [DjangoFilterBackend,OrderingFilter] filterset_fields=['title','addr'] ordering_fields=['price','id']
ordering_param = 'sort'
这个属性不好使
路由
http://127.0.0.1:8000/b2oks/?ordering=-id
标签:ListAPIView,rest,django,framework,过滤,import,排序,class,DRF From: https://www.cnblogs.com/so-shuai/p/17370161.html