当前位置:网站首页>Serializer rewrite: update and create methods
Serializer rewrite: update and create methods
2022-07-03 09:21:00 【weixin_ forty-six million three hundred and seventy-one thousan】
View class :
# How the view class that updates user information calls the serializer
user = models.User.objects.filter(id=2)
ser = seralizer.User_Serialzer(instance=user,data=request.data)
#instance The data object to be operated ,data The data to be modified is passed
# How the view class of the new data calls the serializer
ser = seralizer.User_Serializer(data=request.data)
if ser.is_valid:
# Coming here means passing the verification
Serializer .py
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from testapp import models
class User_Serialzer(serializers.Serializer):
name = serializers.CharField()
password=serializers.CharField()
age = serializers.CharField()
def update(self, instance, validated_data):
#instance Is the object to be modified
#validated_data It is the modified data passed from the front end ,
# Here we need to modify the data one by one
instance.name = validated_data.get('name')
instance.password = validated_data.get('password')
instance.age = validated_data.get('age')
instance.save()
# This modification is a three-step method for modifying data objects ,
'''
1 Get the object ( Instantiate objects )
2 Modify the value through the attribute method
3 You have to use save To preserve
'''
return instance
# Create data , Need to rewrite create Method
def create(self, validated_data):
#validated_data It is the dictionary passed by the front end
obj=models.UserS.objects.create(**validated_data)
if obj:
return validated_data
else:
raise ValidationError(' Failed to create data ')
When is serialization or deserialization :
1、 Get the data object in the database , Passed to serializer , This process is serialization . Is to read the data of the database .
2、 The data collected by the front end is passed to the back end , First pass the basic verification of serializer 、 Hook function verification , Then write to the database . This is the process of deserialization .
边栏推荐
- AcWing 787. Merge sort (template)
- AcWing 785. 快速排序(模板)
- CSDN markdown editor help document
- On February 14, 2022, learn the imitation Niuke project - develop the registration function
- The difference between if -n and -z in shell
- State compression DP acwing 291 Mondrian's dream
- Simple use of MATLAB
- What is an excellent fast development framework like?
- Linxu learning (4) -- Yum and apt commands
- Wonderful review | i/o extended 2022 activity dry goods sharing
猜你喜欢
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Discussion on enterprise informatization construction
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
Low code momentum, this information management system development artifact, you deserve it!
LeetCode 715. Range module
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
Jenkins learning (II) -- setting up Chinese
LeetCode 532. 数组中的 k-diff 数对
State compression DP acwing 291 Mondrian's dream
What are the stages of traditional enterprise digital transformation?
随机推荐
LeetCode 508. 出现次数最多的子树元素和
State compression DP acwing 91 Shortest Hamilton path
Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
LeetCode 438. Find all letter ectopic words in the string
Low code momentum, this information management system development artifact, you deserve it!
Crawler career from scratch (3): crawl the photos of my little sister ③ (the website has been disabled)
Discussion on enterprise informatization construction
一个优秀速开发框架是什么样的?
Wonderful review | i/o extended 2022 activity dry goods sharing
【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
What is the difference between sudo apt install and sudo apt -get install?
【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
[point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
Beego learning - Tencent cloud upload pictures
Save the drama shortage, programmers' favorite high-score American drama TOP10
Crawler career from scratch (IV): climb the bullet curtain of station B through API
Linxu learning (4) -- Yum and apt commands
Matlab dichotomy to find the optimal solution