Bootstrap

ModelViewSet过滤排序分页和增删改查

导包

from app01 import models as models
from app01 import sers as serializers
from rest_framework.viewsets import ModelViewSet
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.pagination import PageNumberPagination

自定义方法类

class UserViewSet(ModelViewSet):

实现基本增删改查操作

    queryset = models.User.objects.all()
    serializer_class = serializers.UserSer

定义局部过滤查询

filter_backends = [DjangoFilterBackend, OrderingFilter]
filter_fields = ("name", "age", "home")
ordering_fields = ("age", "height")

分页

#这里自定义分页器
class PeterPage(PageNumberPagination):
    page_size_query_param = "max_page"

    max_page_size = 10
 #这里使用
class UserViewSet(ModelViewSet): 
 	pagination_class = PeterPage
;