当前位置:网站首页>Use of views
Use of views
2022-07-23 11:15:00 【ZXY_ lucky】
View Introduction
APIView —> GenericAPIView —> View extension class —> Subclass view
1.APIView
Import before use
from rest_framework.views import APIView
class StaffDetaView(APIView):
def get(self,request,pk):
return Response({
"code":200,"msg":' To be successful '})
def put(self,request,pk,**kwargs):
# according to id Find the corresponding information , Returns the object
staff = Staff.objects.filter(id=pk).first()
if not staff:
return Response({
"code":400,
"msg":' The modified data does not exist '
})
# Deserialization to update
# partial Implement local update
ser = StaffSerializer(instance=staff, data=request.data, partial=True)
# Check the data
if ser.is_valid():
ser.save() # call update
return Response({
"code": 200,
"msg": ' Modification successful '
})
else:
return Response({
"code":400,"msg":ser.errors})
return self.update(request,pk,**kwargs)
def delete(self,request,pk):
return Response({
"code":200,"msg":' Delete employee succeeded '})
2.GenericAPIView
Inherited from APIView
from rest_framework.generics import GenericAPIView
General properties
- queryset View query set
- serializer_class The serializer used by the view
List view uses :
- pagination_class Paging control class
- filter_backends Filter control back end , Filter fields
The detail page view uses
- lookup_field The condition field used when querying a single data object , The default is ’pk’
- lookup_url_kwarg When querying single data URL Keyword parameters in , Default and look_field identical
Common methods :
- get_queryset()
- Returns the query set used by the view
- Default return queryset attribute , Support to rewrite - get_serializer_class()
- Returns the serializer class , Default return serializer_class, Can be rewritten - get_serializer(self, *args, **kwargs)
- Returns the serializer object
Details page unique method
- get_object()
- Default parameters to filter
class StaffView(GenericAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def get(self,request,*args,**kwargs):
staff_info = self.get_queryset()
# serialize , Return a serialized object
staff_all = self.get_seializer(staff_info,many=True)
return Response({
"staff":staff_all.data
})
3. The extension class
| The extension class | Encapsulation method | explain |
|---|---|---|
| ListModelMixin | List | Get a set of data |
| CreateModelMixin | create | Add a piece of data |
| RetrieveModelMixin | retrieve | Get the specified data |
| UpdateModelMixin | update | Update specified data |
| DestroyModelMixin | destroy | Delete specified data |
class StaffView(GenericAPIView,ListModelMixin):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
def get(self,request,*args,**kwargs):
return self.list(request,*args,**kwargs)
4. View subclass
| Subclass view class | Inherit | Request processing method |
|---|---|---|
| ListAPIView | GenericAPIView、ListModelMixin | get |
| CreateAPIView | GenericAPIView、CreateModelMixin | post |
| RetrieveAPIView | GenericAPIView、RetrieveModelMixin | get |
| UpdateAPIView | GenericAPIView、UpdateModelMixin | put |
| DestroyAPIView | GenericAPIView、DestroyModelMixin | delete |
| ListCreateAPIView | GenericAPIView、ListModelMixin、CreateModelMixin | get and post |
| RetrieveUpdateAPIView | GenericAPIView、RetrieveModelMixin、UpdateModelMixin | get、put |
| RetrieveDestroyAPIView | GenericAPIView、RetrieveModelMixin、DestroyModelMixin | get and delete |
| RetrieveUpdateDestroyAPIView | GenericAPIView、RetrieveModelMixin、UpdateModelMixin、DestroyModelMixin | get、put、delete |
Import
from rest_framework.generics import *
class StaffView(ListCreateAPIView):
queryset = Staff.objects.all()
serializer_class = StaffSerializer
notes : This is an example , Other view methods are the same
边栏推荐
猜你喜欢
随机推荐
【6.28】
C#的partial用法
PyGame realizes the airplane war game
check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ord
img标签设置height和width无效
MySQL statement queries all child nodes of a level node
图片模糊处理批量生产模糊数据集
Spark常见面试问题整理
【无标题】
Alipay DTS architecture
【无标题】
【无标题】
JDBC learning and simple encapsulation
十年架构五年生活-01毕业之初
[python flask notes 5] blueprint is easy to use
【Pyradiomics】提取的影像组学特征值不正常(很多0和1)
Data Lake: introduction to delta Lake
[flink]flink on yarn之flink-conf最简单配置
shell/sh/bash的区别和基本操作
从零开始的pytorch小白使用指北









