当前位置:网站首页>drf serializer - Serializer
drf serializer - Serializer
2022-08-02 14:20:00 【Spaghetti Mixed with No. 42 Concrete】
Python之drf序列化器-Serializer
一、序列化器-Serializer的作用
- 序列化,The serializer converts model class objects into dictionaries,经过Response变成json字符串
- 反序列化,data sent by the client,经过request以后变成字典,Serializers can convert dictionaries into models
- 反序列化,Verify the legitimacy of the data sent by the client
二、定义序列化器
- Django REST framework中的Serializer使用类来定义,须继承自rest_framework.serializers.Serializer.
You need to create a model class in the database first(建表)
from django.db import models
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
在应用下新建serializer.py文件(名字可以随意取,一般使用serializer),在文件中导入刚才建立的表并导入serializers.如下
from rest_framework import serializers
from .models import Student
# 定义序列化器
class StudentSerializer(serializers.Serializer):
# Write the field name you want to serialize below
name = serializers.CharField()
age = serializers.IntegterField()
The above is done as soon as a serialization class is written,下一步在view.pyFill in the corresponding code in the view function.如下
from django.shortcuts import render
# Create your views here.
import models
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import StudentSerializes
class Student(APIView);
def get(self,request):
back_dic = {
'code':200,'msg':'成功','result':''}
queryset_obj = models.Student.objects.all()
res = StudentSerializers(instance=query_obj,many=True)
if not res.is_valid():
back_dic['code'] = 1001
back_dic['msg'] = '失败'
return Response(back_dic)
back_dic['result'] = res.data
return Response(back_dic)
此时使用postmanWhen the interface is called, the data stored in the database can be obtained and the data is obtainedjsonformat sent to the front end
This data is from a dictionary orQueryset对象转为json的操作,is the serialization classStudentSerializes做的
# Serializer的构造方法为:
Serializer(instance=None, data=empty, **kwarg)
""" 1)用于序列化时,将模型类对象传入instance参数 2)用于反序列化时,将要被反序列化的数据传入data参数 3)除了instance和data参数外,在构造Serializer对象时,还可通过context参数额外添加数据 """
serializer = AccountSerializer(account, context={
'request': request})
""" 1、使用序列化器的时候一定要注意,序列化器声明了以后,不会自动执行,需要我们在视图中进行调用才可以. 2、序列化器无法直接接收数据,需要我们在视图中创建序列化器对象时把使用的数据传递过来. 3、序列化器的字段声明类似于我们前面使用过的表单系统. 4、开发restful api时,序列化器会帮我们把模型数据转换成字典. 5、drf提供的视图会帮我们把字典转换成json,或者把客户端发送过来的数据转换字典. """
**注意:serializer不是只能为数据库模型类定义,也可以为非数据库模型类的数据定义.serializer是独立于数据库之外的存在.
三、Serializer common fields and their parameters
常用字段:
字段 | 字段构造方式 |
---|---|
BooleanField | BooleanField() |
NullBooleanField | NullBooleanField() |
CharField | CharField(max_length=None, min_length=None, allow_blank=False, trim_whitespace=True) |
EmailField | EmailField(max_length=None, min_length=None, allow_blank=False) |
RegexField | RegexField(regex, max_length=None, min_length=None, allow_blank=False) |
SlugField | SlugField(maxlength=50, min_length=None, allow_blank=False) 正则字段,验证正则模式 [a-zA-Z0-9-]+ |
URLField | URLField(max_length=200, min_length=None, allow_blank=False) |
UUIDField | UUIDField(format=’hex_verbose’) format: 1) 'hex_verbose' 如"5ce0e9a5-5ffa-654b-cee0-1238041fb31a" 2) 'hex' 如 "5ce0e9a55ffa654bcee01238041fb31a" 3)'int' - 如: "123456789012312313134124512351145145114" 4)'urn' 如: "urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a" |
IPAddressField | IPAddressField(protocol=’both’, unpack_ipv4=False, **options) |
IntegerField | IntegerField(max_value=None, min_value=None) |
FloatField | FloatField(max_value=None, min_value=None) |
DecimalField | DecimalField(max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None) max_digits: 最多位数 decimal_palces: 小数点位置 |
DateTimeField | DateTimeField(format=api_settings.DATETIME_FORMAT, input_formats=None) |
DateField | DateField(format=api_settings.DATE_FORMAT, input_formats=None) |
TimeField | TimeField(format=api_settings.TIME_FORMAT, input_formats=None) |
DurationField | DurationField() |
ChoiceField | ChoiceField(choices) choices与Django的用法相同 |
MultipleChoiceField | MultipleChoiceField(choices) |
FileField | FileField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL) |
ImageField | ImageField(max_length=None, allow_empty_file=False, use_url=UPLOADED_FILES_USE_URL) |
ListField | ListField(child=, min_length=None, max_length=None) |
DictField | DictField(child=) |
选项参数:
参数名称 | 作用 |
---|---|
max_length | 最大长度 |
min_lenght | 最小长度 |
allow_blank | 是否允许为空 |
trim_whitespace | 是否截断空白字符 |
max_value | 最小值 |
min_value | 最大值 |
通用参数:
参数名称 | 说明 |
---|---|
read_only | 表明该字段仅用于序列化输出,默认False |
write_only | 表明该字段仅用于反序列化输入,默认False |
required | 表明该字段在反序列化时必须输入,默认True |
default | 反序列化时使用的默认值 |
allow_null | 表明该字段是否允许传入None,默认False |
validators | 该字段使用的验证器 |
error_messages | 包含错误编号与错误信息的字典 |
label | 用于HTML展示API页面时,显示的字段名称 |
help_text | 用于HTML展示API页面时,显示的字段帮助提示信息·1· |
四、Serializer advanced usage
1、source用法
from rest_framework import serializers
from .models import Student
# 定义序列化器
class StudentSerializer(serializers.Serializer):
# Write the field name you want to serialize below
name = serializers.CharField()
age = serializers.IntegterField()
Serialization abovename字段中,This can be used if you don't want the front end to see the actual fields in your tablesource方法,
soucreThe method is that the current name is mapped to a field in this table,In this way, the current name can get the value of the mapped field in the table in the front end,如下
name1 = serializers.CharField(source='name',read_only=True )
name = serializers.ChsrField(write_only=True)
常配合read_only与write_only使用
2、serializers.SerializerMethodField用法
SerializerMethodFieldThe display style of serialized fields can be specified
- 在django2.0以上,It needs to be added when the fields of the model class are associated with other keyson_delete=models.CASCADE(级联删除)
# new model sheet
from django.db import models
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=32)
age = models.IntegerField()
publish = models.ForeignKey(to='publish',on_delete=models.CASCADE)
class Publish(model.Model):
name = models.CharField(max_length=32)
addr = models.CharField(max_length=256)
Fields associated with foreign keys are in the front end(postman)is shown in the object
SerializerMethodFieldmethod can be used to display detailed data associated with foreign keys
# 此时在serializer.py中就需要用到SerializerMethodField方法了
from rest_framework import serializers
from .models import Student
# 定义序列化器
class StudentSerializer(serializers.Serializer):
# Write the field name you want to serialize below
name = serializers.CharField()
age = serializers.IntegterField()
# 方法一(在序列化类中写)
pulish = serializers.SerializerMethodField() # 缺点 Can only be used for serialization 默认就是read_only=True
def get_publish(self,obj): # get_字段名
# return obj.publish.name # Displays the publisher name
return {
'id':obj.publish.id,'name':obj.publish.name,'addr':obj.publish.addr} # Return to publisher details
# 方式二(Write in the model class,方法返回什么,The fields below are what) 用的多
pulish_detail = serializers.CharField() # This return to the front end is in the form of a dictionary,不是json格式的
pulish_detail = serializers.DictField() # 使用DictField返回的就是json格式
# Write a property in the model class
@property
def pulish_detail(self):
return {
'id':self.publish.id,'name':self.publish.name,'addr':self.publish.addr}
# 方式三(了解)
pulish_detail = serializers.CharField(source='publish.name')
五、反序列化
一、数据验证
使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象.
在获取反序列化的数据前,必须调用**is_valid()**方法进行验证,验证成功返回True,否则返回False.
验证失败,可以通过序列化器对象的errors属性获取错误信息,返回字典,包含了字段和字段的错误.如果是非字段错误,可以通过修改REST framework配置中的NON_FIELD_ERRORS_KEY来控制错误字典中的键名.
验证成功,可以通过序列化器对象的validated_data属性获取数据.
在定义序列化器时,指明每个字段的序列化类型和选项参数,本身就是一种验证行为.
1、自定义验证
validate_字段名(也叫局部钩子)
# validate_字段名 Validate a single field
# Validate in the serialization class
def validate_name(self,value)
if '草' in value:
raise ValidationError('不符合社会主义核心价值观')
return value
validate(也叫全局钩子)
# validateWhen comparing multiple fields at the same time,可以定义validate方法来验证
def validate(self, attrs):
bread = attrs['bread']
bcomment = attrs['bcomment']
if bread < bcomment:
raise serializers.ValidationError('阅读量小于评论量')
return attrs
validators
# 在字段中添加validators选项参数,也可以补充验证行为,如
btitle = serializers.CharField(label='名称', max_length=20, validators=[about_django])
二、保存数据
After the previous verification data is successful,我们可以使用序列化器来完成数据反序列化的过程.这个过程可以把数据转成模型类对象.
可以通过重写create()和update()两个方法来实现
class BookInfoSerializer(serializers.Serializer):
"""图书数据序列化器"""
...
def create(self, validated_data):
"""新建"""
res = BookInfo.objects.create(**validated_data)
return res
def update(self, instance, validated_data):
"""更新,instance为要更新的对象实例"""
instance.btitle = validated_data.get('btitle', instance.btitle)
instance.bpub_date = validated_data.get('bpub_date', instance.bpub_date)
instance.bread = validated_data.get('bread', instance.bread)
instance.bcomment = validated_data.get('bcomment', instance.bcomment)
instance.save()
return instance
六、模型类序列化器
在上面的代码中,StudentSerializerSerializer and model classStudent是没有关系的.If light wrote oneStudentSerializer,There are multiple tables in the model classname字段的话,He will serialize all the tablesname字段.
下面使用ModelSerializerSerialization classes can be refactored,It is assigned to a certain class for use
from rest_framework import serializers
from .models import Book
class BookSerializers(serializers.ModelSerializers):
# The corresponding relationship with the table needs to be specified
class Meta:
model = Book # 必须叫model
# 指定要序列化的字段
fields = ['name','price'] # The first is written as a list specifying the fields to serialize
fields = '__all__' # 第二种'__all__'Serialize all fields in the table
""" 也可以使用exclude可以明确排除掉哪些字段 可以通过read_only_fields指明只读字段,即仅用于序列化输出的字段 """
read_only_fields = ('id', 'bread', 'bcomment')
添加额外参数选项
我们可以使用extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
class BookInfoSerializer(serializers.ModelSerializer):
"""图书数据序列化器"""
class Meta:
model = BookInfo
fields = ('id', 'btitle', 'bpub_date', 'bread', 'bcomment')
extra_kwargs = {
'bread': {
'min_value': 0, 'required': True},
'bcomment': {
'min_value': 0, 'required': True},
}
depth
# When there is a foreign key in a relational table,When querying, the foreign key field value will appear as oneid号
# If you want the foreign key field to display detailed information, you need to be in
class Meta:
……
depth=1 # 这个1Indicates the number of nesting levels
七、习题
""" 1 定义两个表,Books and Publishers table -出版社的5个接口 -图书的5个接口 -查询的时候,Publisher shows details -Added and modified band publishers -The price is displayed as xx元 2 When adding a publisher,addr最大8,最小3,不能以sb开头,The address cannot contain a first name """
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
book_name = models.CharField(max_length=32,verbose_name='书名')
book_price = models.IntegerField(verbose_name='价钱')
publish = models.ForeignKey(to='Publish',on_delete=models.CASCADE)
# Inquiry with publisher details Method 2(Write directly in the model class)
@property
def get_publish(self):
return {
'id':self.publish.id,'publish_name':self.publish.publish_name,'publish_addr':self.publish.publish_addr}
# Inquiry price method 2(Write directly in the model class)
@property
def get_book_price(self):
return str(self.book_price)+'元'
class Publish(models.Model):
publish_name = models.CharField(max_length=32,verbose_name='出版社名称')
publish_addr = models.CharField(max_length=255,verbose_name='出版社地址')
serializer.py
from rest_framework import serializers
from .models import Publish, Book
from rest_framework.exceptions import ValidationError # The exception status package needs to be imported,djangoA global exception will be caught
class PublishModelSerializer(serializers.ModelSerializer):
class Meta:
model = Publish
fields = ['id', 'publish_name', 'publish_addr']
# extra_kwargs = {
# 'publish_addr': {'max_length': 8, 'min_length': 3, 'error_messages': {
# 'min_length': '太短',
# 'max_length': '太长'
# }}
# }
#全局钩子
def validate(self, attrs):
print(attrs)
if attrs.get('publish_addr').startswith('sb'):
raise ValidationError('不符合社会主义核心价值观')
if attrs.get('publish_name') in attrs['publish_addr']:
raise ValidationError('The address cannot contain a first name')
return attrs
class BookModelSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ['id', 'book_name', 'book_price', 'publish', 'get_book_price', 'get_publish']
extra_kwargs = {
'id': {
'read_only': True},
'book_price': {
'write_only': True},
'publish': {
'write_only': True}
}
# When deserializing, you want to accept onlybook_price,publishTwo fields can limit it towrite_only = True
# book_price = serializers.IntegerField(write_only=True)
# publish = serializers.IntegerField(write_only=True)
# Inquiry with publisher details method 1
# publish = serializers.SerializerMethodField()
# def get_publish(self,obj):
# return {'id':obj.publish.id,'publish_name':obj.publish.publish_name,'publish_addr':obj.publish.publish_addr}
# The price is displayed as xxMeta method one
# book_price = serializers.SerializerMethodField()
# def get_book_price(self,obj):
# return str(obj.book_price)+'元'
# 序列化的时候使用book_price,publish,It is when deserializingobook_price,opublish
# get_book_price = serializers.CharField(read_only=True)
# get_publish = serializers.CharField(read_only=True)
views.py
from django.shortcuts import render
# Create your views here.
""" 1 定义两个表,Books and Publishers table -出版社的5个接口 -图书的5个接口 -查询的时候,Publisher shows details -Added and modified band publishers -The price is displayed as xx元 2 When adding a publisher,addr最大8,最小3,不能以sb开头,The address cannot contain a first name """
from app01 import models
from rest_framework.views import APIView
from .serializers import PublishModelSerializer,BookModelSerializer
from rest_framework.response import Response
from rest_framework.exceptions import ValidationError
# 出版社 无参
class publish(APIView):
# Get all publisher interfaces
def get(self,request):
back_dic = {
'code':200,'msg':'查询成功','result':''}
res = models.Publish.objects.all()
result = PublishModelSerializer(instance=res,many=True)
back_dic['result'] = result.data
return Response(back_dic)
def post(self,request):
back_dic = {
'code': 200, 'msg': '新增成功', 'result': ''}
result = PublishModelSerializer(data=request.data)
if not result.is_valid():
return Response(result.errors)
result.save() # 保存数据
back_dic['result'] = result.data
return Response(back_dic)
# 有参
class publish_param(APIView):
def get(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '查询成功', 'result': ''}
res = models.Publish.objects.filter(**kwargs).first()
result = PublishModelSerializer(instance=res)
back_dic['result'] = result.data
return Response(back_dic)
def delete(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '删除成功'}
models.Publish.objects.filter(**kwargs).delete()
return Response(back_dic)
def put(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '修改成功', 'result': ''}
res = models.Publish.objects.filter(**kwargs).first()
result = PublishModelSerializer(instance=res,data=request.data)
if result.is_valid() ==False:
return Response(result.errors)
result.save()
back_dic['result'] = result.data
return Response(back_dic)
# 书籍 无参
class Book(APIView):
# Get all book interfaces
def get(self, request):
back_dic = {
'code': 200, 'msg': '查询成功', 'result': ''}
res = models.Book.objects.all()
result = BookModelSerializer(instance=res, many=True)
back_dic['result'] = result.data
return Response(back_dic)
def post(self, request):
back_dic = {
'code': 200, 'msg': '新增成功', 'result': ''}
result = BookModelSerializer(data=request.data)
if not result.is_valid():
return Response(result.errors)
result.save() # 保存数据
back_dic['result'] = result.data
return Response(back_dic)
class Book_param(APIView):
def get(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '查询成功', 'result': ''}
res = models.Book.objects.filter(**kwargs).first()
result = BookModelSerializer(instance=res)
back_dic['result'] = result.data
return Response(back_dic)
def delete(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '删除成功'}
models.Book.objects.filter(**kwargs).delete()
return Response(back_dic)
def put(self,request,**kwargs):
back_dic = {
'code': 200, 'msg': '修改成功', 'result': ''}
res = models.Book.objects.filter(**kwargs).first()
result = BookModelSerializer(instance=res,data=request.data)
if result.is_valid() ==False:
return Response(result.errors)
result.save()
back_dic['result'] = result.data
return Response(back_dic)
urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
# 出版社
path('publish/', views.publish.as_view()),
path('publish/<int:pk>', views.publish_param.as_view()),
path('book/', views.Book.as_view()),
path('book_param/<int:pk>', views.Book_param.as_view()),
]
边栏推荐
- Flask框架
- [ROS] (06) ROS Communication - Topic Communication
- chapter7
- Supervision strikes again, what about the market outlook?2021-05-22
- MarkDown语法汇总
- MySQL数据库语法格式
- [ROS] Compiling packages packages encounters slow progress or stuck, use swap
- [ROS] (04) Detailed explanation of package.xml
- VMM是什么?_兮是什么意思
- php开源的客服系统_在线客服源码php
猜你喜欢
动态刷新日志级别
How to solve 1045 cannot log in to mysql server
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
WeChat Mini Program-Recent Dynamic Scrolling Implementation
8580 合并链表
使用云GPU+pycharm训练模型实现后台跑程序、自动保存训练结果、服务器自动关机
[ROS](06)ROS通信 —— 话题(Topic)通信
shell脚本“画画”
window10下半自动标注
Supervision strikes again, what about the market outlook?2021-05-22
随机推荐
What's wrong with running yolov5 (1) p, r, map are all 0
Supervision strikes again, what about the market outlook?2021-05-22
Audio processing: floating point data stream to PCM file
使用云GPU+pycharm训练模型实现后台跑程序、自动保存训练结果、服务器自动关机
Paddle window10 environment using conda installation
Haystack的介绍和使用
Sentinel源码(一)SentinelResourceAspect
Minio文件上传
ZABBIX配置邮件报警和微信报警
编程规范——LiteOS
[ROS](05)ROS通信 —— 节点,Nodes & Master
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
How to solve 1045 cannot log in to mysql server
云GPU(恒源云)训练的具体操作流程
drf源码分析与全局捕获异常
第十单元 前后连调
EasyExcel 的使用
[ROS](03)CMakeLists.txt详解
一维卷积神经网络_卷积神经网络的基础知识「建议收藏」
8581 线性链表逆置