简单视图处理
urls.py
from django.conf.urls import url
from api import views
app_name = 'api'
urlpatterns = [
url(r'^(?P[v1|v2]+)/view/$', views.ViewView.as_view(), name='view'),
]
views.py
from rest_framework.views import APIView
class ViewView(APIView):
def get(self, request, *args, **kwargs):
return Response('视图处理')
规范化视图操作
views.py
from rest_framework.generics import GenericAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from api.models import Role
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
class MyPageSerializer(PageNumberPagination):
'''自定义分页类'''
# 设置显示页数的参数
page_query_param = 'page' # ?page=3&size=3 中的page名
# 设置每页显示数量,每页显示三条数据
page_size = 3
# 设置每页显示数量的参数
page_size_query_param = 'size' # ?page=3&size=3 中的size名
# 设置每页最大显示数量
max_page_size = 5
class ViewView(GenericAPIView):
queryset = Role.objects.all()
serializer_class = PageSerializer
pagination_class = MyPageSerializer
def get(self, request, *args, **kwargs):
role_queryset = self.get_queryset()
pager_roles = self.paginate_queryset(role_queryset)
ser = self.get_serializer(instance=pager_roles, many=True)
return Response(ser.data)
重写as_view()方法
将指定请求方法指定到对应函数
可以再路由上指定对应的函数
即不同url可以指定同一个视图类;并且需要指定url请求方法对应相应的函数
urls.py
from django.conf.urls import url
from api import views
app_name = 'api'
urlpatterns = [
url(r'^(?P[v1|v2]+)/view/$', views.ViewView.as_view({'get': 'list'}),name='view'),
]
views.py
from rest_framework.viewsets import GenericViewSet
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from api.models import Role
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
class MyPageSerializer(PageNumberPagination):
'''自定义分页类'''
page_query_param = 'page'
page_size = 3
page_size_query_param = 'size'
max_page_size = 5
class ViewView(GenericViewSet):
queryset = Role.objects.all()
serializer_class = PageSerializer
pagination_class = MyPageSerializer
def list(self, request, *args, **kwargs):
role_queryset = self.get_queryset()
pager_roles = self.paginate_queryset(role_queryset)
ser = self.get_serializer(instance=pager_roles, many=True)
return Response(ser.data)
列出所有数据
urls.py
from django.conf.urls import url
from api import views
app_name = 'api'
urlpatterns = [
url(r'^(?P[v1|v2]+)/view/$', views.ViewView.as_view({'get': 'list'}),name='view'),
]
views.py
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from api.models import Role
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
class MyPageSerializer(PageNumberPagination):
'''自定义分页类'''
page_query_param = 'page'
page_size = 3
page_size_query_param = 'size'
max_page_size = 5
class ViewView(ListModelMixin, GenericViewSet):
queryset = Role.objects.all()
serializer_class = PageSerializer
pagination_class = MyPageSerializer
url:http://127.0.0.1:8000/api/v1/view/?page=1
结果
{
"count": 8,
"next": "http://127.0.0.1:8000/api/v1/view/?page=2",
"previous": null,
"results": [
{
"id": 1,
"title": "医生"
},
{
"id": 2,
"title": "护士"
},
{
"id": 3,
"title": "老师"
}
]
}
添加数据
上述继承的CreateModelMixin和ListModelMixin 不需要传参数id
urls.py
from django.conf.urls import url
from api import views
app_name = 'api'
urlpatterns = [
url(r'^(?P[v1|v2]+)/view/$', views.ViewView.as_view({'get': 'list', 'post': 'create'}), name='view'),
]
下面继承的三个类:RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin 需要传参数id
urls.py
from django.conf.urls import url
from api import views
app_name = 'api'
urlpatterns = [
url(r'^(?P[v1|v2]+)/view/$', views.ViewView.as_view({'get': 'list', 'post': 'create'})),
url(r'^(?P[v1|v2]+)/view/(?P\d+)/$',
views.ViewView.as_view({'get': 'retrieve', 'put': 'update', 'patch': 'partial_update', 'delete': 'destroy'})
),
]
获取一条数据
更新数据
删除一条数据
ModelViewSet继承上诉所有功能
views.py
from rest_framework.viewsets import ModelViewSet
from rest_framework.pagination import PageNumberPagination
from rest_framework import serializers
from rest_framework.response import Response
from api.models import Role
class PageSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = '__all__'
class ViewView(ModelViewSet):
queryset = Role.objects.all()
serializer_class = PageSerializer
pagination_class = PageNumberPagination