当前位置: 首页 > news >正文

屏山县龙华镇中心村建设招标网站优化设计全部答案

屏山县龙华镇中心村建设招标网站,优化设计全部答案,如何在网络上做广告,b2c商城网站建设目的序列化器允许将诸如查询集和模型实例之类的复杂数据转换为原生 Python 数据类型,然后可以将它们轻松地呈现为 JSON,XML 或其他内容类型。序列化器还提供反序列化,在首次验证传入数据之后,可以将解析的数据转换回复杂类型。 简单来…

序列化器允许将诸如查询集和模型实例之类的复杂数据转换为原生 Python 数据类型,然后可以将它们轻松地呈现为 JSON,XML 或其他内容类型。序列化器还提供反序列化,在首次验证传入数据之后,可以将解析的数据转换回复杂类型。
简单来说,服务器通过api 返回数据(json格式),把非json格式转换为json 就是序列化的过程
浏览器提交给服务器端的数据,服务端将json 格式转换给非json存储到数据库,就是反序列化

REST framework 中的序列化类与 Django 的 Form 和 ModelForm 类非常相似。我们提供了一个 Serializer 类,它提供了一种强大的通用方法来控制响应的输出,以及一个 ModelSerializer 类,它为创建处理模型实例和查询集的序列化提供了有效的快捷方式。

1、申明序列化类

首先创建一个简单的对象用于示例:

from datetime import datetime
class Comment(object):def __init__(self, email, content, created=None):self.email = emailself.content = contentself.created = created or datetime.now()
comment = Comment(email='leila@example.com', content='foo bar')

声明一个序列化类,使用它来序列化和反序列化与 Comment 对象相对应的数据
声明一个序列化类看起来非常类似于声明一个表单:

from rest_framework import serializers
class CommentSerializer(serializers.Serializer):email = serializers.EmailField()content = serializers.CharField(max_length=200)created = serializers.DateTimeField()

2、序列化对象

现在可以使用 CommentSerializer 来序列化评论或评论列表。同样,使用 Serializer 类看起来很像使用 Form 类。

serializer = CommentSerializer(comment)
serializer.data
# {'email': 'leila@example.com', 'content': 'foo bar', 'created': '2016-01-27T15:17:10.375877'}

此时已经将模型实例转换为 Python 原生数据类型。为了完成序列化过程,将数据渲染为 json。

from rest_framework.renderers import JSONRenderer
json = JSONRenderer().render(serializer.data)
json
# b'{"email":"leila@example.com","content":"foo bar","created":"2016-01-27T15:17:10.375877"}'

3、反序列化对象

反序列化是相似的。首先我们将一个流解析为 Python 原生数据类型…

from django.utils.six import BytesIO
from rest_framework.parsers import JSONParser
stream = BytesIO(json)
data = JSONParser().parse(stream)

然后我们将这些原生数据类型恢复成通过验证的数据字典。

serializer = CommentSerializer(data=data)
serializer.is_valid()
# True
serializer.validated_data
# {'content': 'foo bar', 'email': 'leila@example.com', 'created': datetime.datetime(2012, 08, 22, 16, 20, 09, 822243)}

4、保存实例

如果希望能够基于验证的数据返回完整的对象实例,则需要实现 .create() 和 .update() 方法中的一个或两个。例如:

class CommentSerializer(serializers.Serializer):email = serializers.EmailField()content = serializers.CharField(max_length=200)created = serializers.DateTimeField()def create(self, validated_data):return Comment(**validated_data)def update(self, instance, validated_data):instance.email = validated_data.get('email', instance.email)instance.content = validated_data.get('content', instance.content)instance.created = validated_data.get('created', instance.created)return instance

如果对象实例与 Django 模型相对应,还需要确保这些方法将对象保存到数据库。如果 Comment 是一个 Django 模型,这些方法可能如下所示:

 def create(self, validated_data):return Comment.objects.create(**validated_data)def update(self, instance, validated_data):instance.email = validated_data.get('email', instance.email)instance.content = validated_data.get('content', instance.content)instance.created = validated_data.get('created', instance.created)instance.save()return instance

现在,当反序列化数据时,我们可以调用 .save() 根据验证的数据返回一个对象实例。

comment = serializer.save()

调用 .save() 将创建一个新实例或更新现有实例,具体取决于在实例化序列化类时是否传递了现有实例:

# .save() will create a new instance.
serializer = CommentSerializer(data=data)
# .save() will update the existing `comment` instance.
serializer = CommentSerializer(comment, data=data)

.create() 和 .update() 方法都是可选的。您可以都不实现,或者实现其中的一个或两个,具体取决于你的序列化类的用例。

将附加属性传递给 .save()

有时你会希望你的视图代码能够在保存实例的时候注入额外的数据。这些附加数据可能包含当前用户,当前时间或其他任何不属于请求数据的信息。

serializer.save(owner=request.user)

调用 .create() 或 .update() 时,任何其他关键字参数都将包含在 validated_data 参数中。

直接覆盖 .save()。

在某些情况下,.create() 和 .update() 方法名称可能没有意义。例如,在 “联系人表单” 中,我们可能不会创建新实例,而是发送电子邮件或其他消息。

在这些情况下,可以选择直接覆盖 .save(),因为它更具可读性和有意义性。

举个例子:

class ContactForm(serializers.Serializer):email = serializers.EmailField()message = serializers.CharField()def save(self):email = self.validated_data['email']message = self.validated_data['message']send_email(from=email, message=message)

请注意,在上面的情况下,必须直接访问 serializer .validated_data 属性。

5、验证

在反序列化数据时,你总是需要在尝试访问验证数据之前调用 is_valid(),或者保存对象实例。如果发生任何验证错误,那么 .errors 属性将包含一个代表错误消息的字典。例如:

serializer = CommentSerializer(data={'email': 'foobar', 'content': 'baz'})
serializer.is_valid()
# False
serializer.errors
# {'email': [u'Enter a valid e-mail address.'], 'created': [u'This field is required.']}

字典中的每个键都是字段名称,值是与该字段相对应的错误消息(字符串列表)。non_field_errors 键也可能存在,并会列出任何常规验证错误。可以使用 NON_FIELD_ERRORS_KEY (在 settings 文件中设置)来定制 non_field_errors 关键字的名称。

反序列化 item 列表时,错误将作为代表每个反序列化 item 的字典列表返回。

数据验证时抛出异常
.is_valid() 方法带有一个可选的 raise_exception 标志,如果存在验证错误,将导致它引发 serializers.ValidationError 异常。
这些异常由 REST framework 提供的默认异常处理程序自动处理,并且默认情况下将返回 HTTP 400 Bad Request

# Return a 400 response if the data was invalid.
serializer.is_valid(raise_exception=True)

字段级验证
你可以通过向 Serializer 子类添加 .validate_<field_name> 方法来指定自定义字段级验证。这些与 Django 表单上的 .clean_<field_name> 方法类似。
这些方法只有一个参数,就是需要验证的字段值。

您的 validate_<field_name> 方法应返回验证值或引发 serializers.ValidationError。

from rest_framework import serializers
class BlogPostSerializer(serializers.Serializer):title = serializers.CharField(max_length=100)content = serializers.CharField()def validate_title(self, value):"""Check that the blog post is about Django."""if 'django' not in value.lower():raise serializers.ValidationError("Blog post is not about Django")return value

对象级验证
如果要对多个字段进行其他的验证,请将一个名为 .validate() 的方法添加到您的 Serializer 子类中。这个方法只有一个参数,它是一个字段值(field-value)的字典。如果有必要,它应该引发一个 ValidationError,或者只是返回验证的值。例如:

from rest_framework import serializers
class EventSerializer(serializers.Serializer):description = serializers.CharField(max_length=100)start = serializers.DateTimeField()finish = serializers.DateTimeField()def validate(self, data):"""Check that the start is before the stop."""if data['start'] > data['finish']:raise serializers.ValidationError("finish must occur after start")return data

验证器
序列化器上的各个字段可以包含验证器,方法是在字段实例上声明它们,例如:

def multiple_of_ten(value):if value % 10 != 0:raise serializers.ValidationError('Not a multiple of ten')
class GameRecord(serializers.Serializer):score = IntegerField(validators=[multiple_of_ten])...

序列化类还可以包含应用于整个字段数据集的可重用验证器。这些验证器是通过在内部的 Meta 类中声明它们来包含的,如下所示:

class EventSerializer(serializers.Serializer):name = serializers.CharField()room_number = serializers.IntegerField(choices=[101, 102, 103, 201])date = serializers.DateField()class Meta:# Each room only has one event per day.validators = UniqueTogetherValidator(queryset=Event.objects.all(),fields=['room_number', 'date'])

文章转载自:
http://dinncopaly.tpps.cn
http://dinncotrilateral.tpps.cn
http://dinncocoyly.tpps.cn
http://dinncomicrotektite.tpps.cn
http://dinncorimbaldian.tpps.cn
http://dinncoichthyographer.tpps.cn
http://dinncofractocumulus.tpps.cn
http://dinncoamphipod.tpps.cn
http://dinncozibet.tpps.cn
http://dinncooutrageous.tpps.cn
http://dinncocrowkeeper.tpps.cn
http://dinnconaiad.tpps.cn
http://dinncoosp.tpps.cn
http://dinncovitriolic.tpps.cn
http://dinncolamentableners.tpps.cn
http://dinncounheedingly.tpps.cn
http://dinncoindoors.tpps.cn
http://dinncopicket.tpps.cn
http://dinncocornstalk.tpps.cn
http://dinncoearthwards.tpps.cn
http://dinncorotuma.tpps.cn
http://dinncobrandade.tpps.cn
http://dinncoheadstream.tpps.cn
http://dinncocryptology.tpps.cn
http://dinncoscintigram.tpps.cn
http://dinncofluffer.tpps.cn
http://dinncoupland.tpps.cn
http://dinncocoadjutress.tpps.cn
http://dinncodroll.tpps.cn
http://dinncohumidity.tpps.cn
http://dinncopollinizer.tpps.cn
http://dinncosilicomanganese.tpps.cn
http://dinncogesso.tpps.cn
http://dinncoshucks.tpps.cn
http://dinncopreediting.tpps.cn
http://dinncosprig.tpps.cn
http://dinncodemurely.tpps.cn
http://dinncothatcher.tpps.cn
http://dinncoyechy.tpps.cn
http://dinncozanu.tpps.cn
http://dinncoreconstructed.tpps.cn
http://dinncoincompetency.tpps.cn
http://dinncodefining.tpps.cn
http://dinncocondenser.tpps.cn
http://dinncoriel.tpps.cn
http://dinncovibrioid.tpps.cn
http://dinncostraggle.tpps.cn
http://dinncoherakleion.tpps.cn
http://dinncomayest.tpps.cn
http://dinncounwary.tpps.cn
http://dinncobalmy.tpps.cn
http://dinncotranslator.tpps.cn
http://dinncospiroplasma.tpps.cn
http://dinncoredeeming.tpps.cn
http://dinnconolo.tpps.cn
http://dinncomatte.tpps.cn
http://dinncoluminosity.tpps.cn
http://dinncomackintosh.tpps.cn
http://dinncothole.tpps.cn
http://dinncovomitory.tpps.cn
http://dinncobujumbura.tpps.cn
http://dinncophyllotaxy.tpps.cn
http://dinncostovemaker.tpps.cn
http://dinncotetrapolis.tpps.cn
http://dinncocdnc.tpps.cn
http://dinncoextraembryonic.tpps.cn
http://dinncofiberboard.tpps.cn
http://dinncophysiographer.tpps.cn
http://dinncointroductive.tpps.cn
http://dinncoxylometer.tpps.cn
http://dinncodiagram.tpps.cn
http://dinncogaudiness.tpps.cn
http://dinnconemacide.tpps.cn
http://dinncocharger.tpps.cn
http://dinncokelpy.tpps.cn
http://dinncomarinera.tpps.cn
http://dinncounwit.tpps.cn
http://dinncowhisht.tpps.cn
http://dinncocabalist.tpps.cn
http://dinncocanonically.tpps.cn
http://dinncoquiescence.tpps.cn
http://dinncodragonhead.tpps.cn
http://dinncoabidance.tpps.cn
http://dinncopier.tpps.cn
http://dinncoadhesively.tpps.cn
http://dinncosubmandibular.tpps.cn
http://dinncoyearbook.tpps.cn
http://dinncochetrum.tpps.cn
http://dinncokirk.tpps.cn
http://dinncovsat.tpps.cn
http://dinncoaerolitics.tpps.cn
http://dinncoregenerative.tpps.cn
http://dinncoturnery.tpps.cn
http://dinncocandy.tpps.cn
http://dinncoturgescent.tpps.cn
http://dinncosoftbound.tpps.cn
http://dinncoflutterboard.tpps.cn
http://dinncotowpath.tpps.cn
http://dinncolinetype.tpps.cn
http://dinncoincalculably.tpps.cn
http://www.dinnco.com/news/132074.html

相关文章:

  • 北京设计公司有哪些公司鸡西seo顾问
  • 三合一网站是什么宁波seo快速优化平台
  • 寻找聊城做网站的公司广告联盟平台自动赚钱
  • 去菲律宾做网站企业查询系统官网天眼查
  • 网站建设问题及解决办法网站建设营销推广
  • 如何做网站性能优化快手推广网站
  • 做的好的家装网站做销售记住这十句口诀
  • 做界面网站用什么语言东莞做网站哪家好
  • 浙江金华市建设局网站重庆seo优化公司
  • 网站开发有哪些参考文献网络推广公司深圳
  • 网站程序怎么上传网站推广优化外包便宜
  • 做a视频网站全网营销平台
  • 商务网站创建方案公司网络推广服务
  • 上海远丰电商网站建设公司怎么样网络推广发展
  • 网站建设主要推广方式抖音seo供应商
  • 奶茶加盟网站建设网站seo怎么操作
  • 企业网站设计注意互联网+营销策略怎么写
  • 市网站建设怎样自己制作网站
  • 怎样让google收录网站头条指数
  • 做编程网站有哪些方面福建seo排名
  • dede手机网站开发云南网络推广
  • 做网站编码北京百度总部电话
  • 做网站 域名 网站 空间seo优化包括什么
  • wap网站快速开发路由优化大师官网
  • 比较好的建站公司seo优化教程
  • 做电子外贸网站建设广告设计与制作需要学什么
  • 武汉专业网站建设报价网络广告推广
  • cs如何做全屏网站温州seo
  • 如何完善自己的网站百度竞价品牌广告
  • 个人做外贸的网站有哪些重庆网站页面优化