当前位置:网站首页>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 .
边栏推荐
- 一个优秀速开发框架是什么样的?
- 【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks
- Just graduate student reading thesis
- Redis learning (I)
- String splicing method in shell
- Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
- [point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
- Banner - Summary of closed group meeting
- [point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
猜你喜欢
In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"
LeetCode 75. Color classification
Beego learning - Tencent cloud upload pictures
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
Vscode connect to remote server
AcWing 787. Merge sort (template)
【Kotlin学习】类、对象和接口——定义类继承结构
Jenkins learning (III) -- setting scheduled tasks
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
2022-2-14 learning xiangniuke project - generate verification code
随机推荐
Linxu learning (4) -- Yum and apt commands
Basic knowledge of network security
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
Internet Protocol learning record
The "booster" of traditional office mode, Building OA office system, was so simple!
Overview of database system
C language programming specification
LeetCode 513. 找树左下角的值
网络安全必会的基础知识
Sword finger offer II 029 Sorted circular linked list
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
State compression DP acwing 291 Mondrian's dream
Go language - IO project
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
LeetCode 515. Find the maximum value in each tree row
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
【Kotlin学习】类、对象和接口——定义类继承结构
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
Temper cattle ranking problem
[kotlin learning] classes, objects and interfaces - define class inheritance structure