当前位置:网站首页>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 .
边栏推荐
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
- 【点云处理之论文狂读经典版14】—— Dynamic Graph CNN for Learning on Point Clouds
- dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
- AcWing 788. 逆序对的数量
- Vscode编辑器右键没有Open In Default Browser选项
- Solve POM in idea Comment top line problem in XML file
- [point cloud processing paper crazy reading classic version 10] - pointcnn: revolution on x-transformed points
- LeetCode 513. Find the value in the lower left corner of the tree
- Recommend a low code open source project of yyds
- Crawler career from scratch (V): detailed explanation of re regular expression
猜你喜欢

How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?

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

Low code momentum, this information management system development artifact, you deserve it!

网络安全必会的基础知识

LeetCode 715. Range module

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

LeetCode 438. Find all letter ectopic words in the string

LeetCode 715. Range 模块

LeetCode 508. The most frequent subtree elements and
随机推荐
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
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
LeetCode 324. Swing sort II
What is an excellent fast development framework like?
Vscode connect to remote server
LeetCode 75. Color classification
LeetCode 871. Minimum refueling times
LeetCode 515. Find the maximum value in each tree row
Using variables in sed command
LeetCode 532. 数组中的 k-diff 数对
[point cloud processing paper crazy reading frontier version 10] - mvtn: multi view transformation network for 3D shape recognition
LeetCode 532. K-diff number pairs in array
Matlab dichotomy to find the optimal solution
[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
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
Build a solo blog from scratch
Discussion on enterprise informatization construction
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
Temper cattle ranking problem