当前位置:网站首页>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 .
边栏推荐
- [point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds
- LeetCode 532. K-diff number pairs in array
- Linxu learning (4) -- Yum and apt commands
- 网络安全必会的基础知识
- 拯救剧荒,程序员最爱看的高分美剧TOP10
- We have a common name, XX Gong
- LeetCode 508. 出现次数最多的子树元素和
- Overview of database system
- 【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
- [kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals
猜你喜欢

LeetCode 241. Design priorities for operational expressions

【点云处理之论文狂读前沿版10】—— MVTN: Multi-View Transformation Network for 3D Shape Recognition

Beego learning - Tencent cloud upload pictures
![[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds](/img/ee/3286e76797a75c0f999c728fd2b555.png)
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds

2022-2-13 learning xiangniuke project - version control

【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks

Recommend a low code open source project of yyds

Wonderful review | i/o extended 2022 activity dry goods sharing

推荐一个 yyds 的低代码开源项目

剑指 Offer II 091. 粉刷房子
随机推荐
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
Crawler career from scratch (IV): climb the bullet curtain of station B through API
State compression DP acwing 91 Shortest Hamilton path
The difference between if -n and -z in shell
LeetCode 515. 在每个树行中找最大值
LeetCode 532. 数组中的 k-diff 数对
What is the difference between sudo apt install and sudo apt -get install?
一个优秀速开发框架是什么样的?
Internet Protocol learning record
Build a solo blog from scratch
Simple use of MATLAB
Noip 2002 popularity group selection number
Tag paste operator (#)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature
Banner - Summary of closed group meeting
Sword finger offer II 029 Sorted circular linked list
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)