当前位置:网站首页>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 .
边栏推荐
- Temper cattle ranking problem
- MySQL installation and configuration (command line version)
- LeetCode 1089. 复写零
- 低代码起势,这款信息管理系统开发神器,你值得拥有!
- 【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
- Internet Protocol learning record
- Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
- 我們有個共同的名字,XX工
- [point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
- [point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
猜你喜欢

LeetCode 438. 找到字符串中所有字母异位词

传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!

剑指 Offer II 029. 排序的循环链表

In the digital transformation, what problems will occur in enterprise equipment management? Jnpf may be the "optimal solution"

【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?

Instant messaging IM is the countercurrent of the progress of the times? See what jnpf says

AcWing 788. 逆序对的数量

With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode

AcWing 787. Merge sort (template)

2022-2-13 learning the imitation Niuke project - home page of the development community
随机推荐
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
Move anaconda, pycharm and jupyter notebook to mobile hard disk
Beego learning - Tencent cloud upload pictures
2022-2-14 learning xiangniuke project - generate verification code
LeetCode 75. Color classification
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
Simple use of MATLAB
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
Introduction to the usage of getopts in shell
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
With low code prospect, jnpf is flexible and easy to use, and uses intelligence to define a new office mode
剑指 Offer II 029. 排序的循环链表
Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
Computing level network notes
Windows安装Redis详细步骤
Pic16f648a-e/ss PIC16 8-bit microcontroller, 7KB (4kx14)
Bert install no package metadata was found for the 'sacraments' distribution
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks