标签:函数 reg route default pat 序列 ret elf date
class GenericView(GenericAPIView):
queryset = models.Role.objects.all()
serializer_class = serializers.RoleSerializer
pagination_class = PageNumberPagination
def get(self, request):
# 获取数据
data = self.get_queryset()
# 获取分页数据
page_data = self.paginate_queryset(data)
# 获取序列化数据
resp = self.get_serializer(instance=page_data, many=True)
return Response(resp.data)
get_queryset
: 获取查询的queryset对象paginate_queryset
: 获取分页后的对象,需要接受一个queryset对象参数get_serializer
: 序列化数据,制定序列化对象和制定many的参数class GenericView(GenericViewSet):
def list(self, request):
return Response('list')
from django.conf.urls import re_path
from . import views
urlpatterns = [
re_path(r'^index/', views.GenericView.as_view({'get': 'list'})),
]
class GenericView(ModelViewSet):
queryset = models.Role.objects.all()
serializer_class = serializers.RoleSerializer
pagination_class = PageNumberPagination
url
from django.conf.urls import re_path
from . import views
urlpatterns = [
re_path(r'^index/', views.GenericView.as_view({'get': 'list', 'post': 'update'})),
]
全自动生成路由
from django.urls import path, include, re_path
from rest_framework import routers
from . import views
route = routers.DefaultRouter()
route.register('index', views.GenericView)
urlpatterns = [
re_path('', include(route.urls))
]
标签:函数 reg route default pat 序列 ret elf date
原文地址:https://www.cnblogs.com/ivy-blogs/p/11665535.html