当前位置:网站首页>Review of knowledge points
Review of knowledge points
2022-07-23 11:45:00 【დ᭄ꦿ꧔ꦿ℘⸙ 451】
Knowledge review
1. What is serialization
Convert the model class level data into json data type
2. Deserialization
take json The data format is converted to the model class level data format
Normal serializer
1. Write both in the view and in the serializer file
Model serializer
1. Just write in the view
Foreign key serializer
1. Serialize according to foreign keys ( Default )
2. Serialize according to the description
3. Serialize according to the fields you define
The extension class
preparation
1. Create a project
2. The configuration file
3. Write model class
4. Generate migration file
5. Perform migration file
6. Write view
GenericAPIView
characteristic :
1. Provides query sets and serializers
and mixin Use it with
mixin: Operation method is provided
mixin
ListModelMixin
List view extension class , Provide ***list(request,
*args,**kwargs)*** Method to quickly implement the list viewDefault return 200 Status code
The Mixin Of list Method will filter and paginate the data
CreateModelMixin
Create a view extension class , Provide ***create(request,
*args,**kwargs)*** Method to quickly create a view of resourcesSuccessfully returns 201 Status code .
If Serializer For the data sent by the front end Validation failed , return 400 error
RetrieveModelMixin
Detail view The extension class , Provide ***retrieve(request,
*args,**kwargs)*** Method to return individual dataIf the detailed data exists , return 200, Otherwise return to 404
UpdateModelMixin
Update view extension class , Provide ***update(request,
*args,**kwargs)*** MethodAt the same time provide ***partial_update(request,
*args,**kwargs)*** Method , Local update can be achieved .success return 200, Serializer failed to verify data when , return 400 error
DestroyModelMixin
Delete view extension class , Provide ***destroy(request, *args, **kwargs)*** Method
It can be realized quickly Delete an existing data object
Successfully returns 204, There is no return 404
3. The extension class
.1. CreateAPIView
- Provide post Method , You can create a piece of data
2. ListAPIView
- Provide get Method , Multiple pieces of data can be obtained
3. RetireveAPIView
- Provide get Method , Get the details of a specific data
4. DestoryAPIView
- Provide delete Method , You can delete a piece of existing data
5. UpdateAPIView
- Provide put and patch Method , You can update or partially update a piece of data
6.ListCreateAPIView
- Provide post and get Method , You can create a piece of data , Or get list data
7. RetrieveUpdateAPIView
- Provide get、put、patch Method , You can get a piece of data details , You can also update a piece of data
8. RetrieveDestroyAPIView
- Provide get and delete Method , You can get and delete an existing piece of data
9. RetrieveUpdateDestoryAPIView
- Provide get、put、patch、delete Method , It can achieve the acquisition of single data modify Delete to update
As long as cross table data operations are involved , It is best to use ordinary serializers and GenericApiView To do it on your own , No cross table operation , have access to mixin Methods of and extension classes
View class
from django.shortcuts import render
from rest_framework.generics import GenericAPIView
from .models import Cate
from .serializers import CateModelSerializer
from rest_framework.response import Response
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
# Check all the information
# class CateGenericAPIView(GenericAPIView):
# # 1. Indicates the query set of the current view class operation Model class .objects.all() queryset Inquiry and
# queryset = Cate.objects.all()
# # 2. Indicates the serializer that the view class needs to operate
# serializer_class=CateModelSerializer
# # Query data
# def get(self,request):
# try:
# # Get query set , At this point, you can directly call the query set provided by the current view class
# cates = self.get_queryset()
# except Exception as e:
# print(e)
# return Response(
# {"msg":"NOT FOUND"},status=404
# )
# # Create a serialized object Object name = Serializer ()
# ser = self.serializer_class(cates,many = True)
# # Return results
# return Response(ser.data)
# Query all data
# Display all data and add data
class CateMixinView(GenericAPIView,ListModelMixin,CreateModelMixin):
# 1. Indicates the amount query set of the current operation
queryset = Cate.objects.all()
# 2. Indicates the serializer required by the current operation
serializer_class = CateModelSerializer
# ListModelMixin : Encapsulates the list Method , It realizes the acquisition of all data
def get(self,request):
return self.list(request)
# CreateModelMixin : Encapsulates the create Method Add logic, including verification, has been written
def post(self,request):
return self.create(request)
# Query specified data and modify Delete
class CateDetailMixinView(GenericAPIView,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
# 1. Indicates the query set required by the current operation
queryset = Cate.objects.all()
# 2. Indicates the serializer of the operation
serializer_class = CateModelSerializer
# Query specified data
def get(self,request,pk):
# RetrieveModelMixin: Provide retrieve To query individual data
return self.retrieve(request,id=pk)
# Modifying data
def put(self,request,pk):
# UpdateModelMixin: Provides update Method to modify , Including logical verification has been written
return self.update(request,id=pk)
# Delete data
def delete(self,request,pk):
# DestroyModelMixin: Provides destroy Method to delete
return self.destroy(request,id=pk)
边栏推荐
猜你喜欢

Adding environment variables and templates to systemctl service

NepCTF2022 Writeup

文件上传漏洞原理

数仓4.0笔记——用户行为数据采集四

MySQL add, delete, modify, query & advanced query statements

Common bypass methods for file upload vulnerabilities

Sqli lab pass 17-22 notes

數倉4.0筆記——業務數據采集

MySQL functions & views & import and export

Development of digital collection system / introduction of digital collection scheme
随机推荐
数字藏品系统开发:百度AI致敬中国航空
存储过程 两次排序加一次随机取数据
Federal Reserve governor Waller: decentralized finance may eventually change the traditional financial market
Development of digital collection system: enterprise layout meta universe digital collection
Laravel API interface + token authentication login
shell取某一时间范围内月份
[pyautogui learning] screen coordinates and mouse scrolling
NFT digital collection system development: the combination of music and NFT
XML建模
第一个FLINK程序之WordCount
Typescript introduction
[metric] use Prometheus to monitor flink1.13org.apache.flink.metrics
One of scala variables and data types
NepCTF 2022 MISC <签到题>(极限套娃)
NFT数字藏品开发:京东“奇达熊带你游京城”旅游套装
kubesphere haproxy+keepalived (一)
NFT digital collection system development, development trend of Digital Collections
第六届“蓝帽杯”全国大学生网络安全技能大赛-初赛Writeup
数仓4.0笔记——数仓环境搭建—— Yarn配置
美联储理事沃勒:去中心化金融最终可能会改变传统金融市场