drf过滤:
带过滤的接口只有:查询所有
内置过滤类:
http://127.0.0.1:8000/api/v1/books/?search=红
http://127.0.0.1:8000/api/v1/books/?search=11
此类查询类似于模糊查询
from rest_framework.filters import SearchFilter class BookView(GenericViewSet, ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer filter_backends = [SearchFilter] search_fields = ['price']
第三方过滤类:
需要安装第三方模块:pip3 install django-filter
此类查询可以精准查询,但是只有and条件
from django_filters.rest_framework import DjangoFilterBackend class BookView(GenericViewSet, ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerialzier filter_backends = [DjangoFilterBackend] # http://127.0.0.1:8000/api/v1/books/?name=红楼梦&price=45 # 按名字和价格精准匹配 filterset_fields = ['name', 'price']
自定义过滤类:
查询价格在100-200之间的数据
view内:
from .filter import MyFilter class BookView(GenericViewSet, ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerialzier filter_backends = [MyFilter] # 必须配合一个过滤类
过滤类:
class MyBookfilter(filters.BaseFilterBackend): def filter_queryset(self, request, queryset, view): price_min = request.query_params.get('price_min') price_max = request.query_params.get('price_max') queryset = queryset.filter(Q(price__gt=price_min) & Q(price__lt=price_max)) return queryset
排序:
from rest_framework.filters import OrderingFilter class BookView(GenericViewSet, ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerialzier filter_backends = [OrderingFilter] # 排序类 # 配合一个类属性,按哪些字段可以排序 # http://127.0.0.1:8000/api/v1/books/?ordering=-price,-id # 先按价格倒序排,价格一样,再按id倒叙排 ordering_fields = ['price','id']
分页:
基本分页:
view中:
from .page import MyPageNumberPagination class BookView(GenericViewSet, ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer pagination_class = MyPageNumberPagination
page中:
from rest_framework.pagination import PageNumberPagination
class MyPageNumberPagination(PageNumberPagination): page_size = 2 # 每页显示的条数 page_query_param = 'page' # page=4 表示第4页 page_size_query_param = 'page_size' # page=4&page_size=5,表示查询第4页,每页显示5条 max_page_size = 5 # 每页最大显示多少条
偏移分页:
view中:
from .page import MyLimitOffsetPagination class BookView(GenericViewSet,ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer pagination_class =MyLimitOffsetPagination
page中:
from rest_framework.pagination import LimitOffsetPagination
class MyLimitOffsetPagination(LimitOffsetPagination): default_limit = 2 # 每页显示多少条 limit_query_param = 'limit' # limit=3 这一页取3条 offset_query_param = 'offset' # 偏移量是多少 offset=3&limit=2 从第3条开始,拿两条 max_limit = None # 最多取5条
游标分页,只能上一页和下一页,不能直接跳到某一页,但是这个的速度快---》app上用它多
view中:
from .page import MyCursorPagination class BookView(GenericViewSet,ListModelMixin): queryset = Book.objects.all() serializer_class = BookSerializer pagination_class =MyCursorPagination
page中:
from rest_framework.pagination import CursorPagination
class MyCursorPagination(CursorPagination): # 重写几个类属性 :3个 cursor_query_param = 'cursor' # 查询参数,其实用不到 page_size = 2 # 每页显示多少条 ordering = 'id' # 必须是要分页的数据表中的字段,一般按id来
标签:price,queryset,page,filter,过滤,import,排序,class,drf From: https://www.cnblogs.com/Hao12345/p/17432983.html