当前位置:网站首页>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 .
边栏推荐
- Build a solo blog from scratch
- What is an excellent fast development framework like?
- LeetCode 30. 串联所有单词的子串
- Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
- Sword finger offer II 029 Sorted circular linked list
- Wonderful review | i/o extended 2022 activity dry goods sharing
- [point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
- Introduction to the usage of getopts in shell
- [point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
- LeetCode 532. 数组中的 k-diff 数对
猜你喜欢

LeetCode 532. 数组中的 k-diff 数对

LeetCode 324. Swing sort II

Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?

LeetCode 241. Design priorities for operational expressions

【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni

Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!

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

浅谈企业信息化建设

Vscode connect to remote server

Vscode编辑器右键没有Open In Default Browser选项
随机推荐
AcWing 788. Number of pairs in reverse order
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
Jenkins learning (I) -- Jenkins installation
2022-2-13 learn the imitation Niuke project - Project debugging skills
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Digital management medium + low code, jnpf opens a new engine for enterprise digital transformation
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points
Install third-party libraries such as Jieba under Anaconda pytorch
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
精彩回顾|I/O Extended 2022 活动干货分享
String splicing method in shell
图像修复方法研究综述----论文笔记
【点云处理之论文狂读前沿版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 508. 出现次数最多的子树元素和
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
2022-2-14 learning xiangniuke project - generate verification code
[point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks