django序列化serializers过程解析

yipeiwu_com5年前Python基础

这篇文章主要介绍了django序列化serializers过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

DRF的核心 就是 前后端分离的核心

前后端分离开发的核心:

  • 将模型转换为json 称之为 序列化
  • 将json转换为模型 称之为 反序列化

Django的序列化工具让你可以将Django的模型‘翻译'成其它格式的数据。通常情况下,这种其它格式的数据是基于文本的,并且用于数据交换\传输过程。

序列化数据

Django为我们提供了一个强大的序列化工具serializers。使用它也很简单,如下所示:

from django.core import serializers
data = serializers.serialize("xml", SomeModel.objects.all())
data = serializers.serialize("json", Author.objects.all())
print(data)

[{"model": "index.author", "pk": 1, "fields": {"name": "pfeiliu", "age": 19, "email": "789@163.com", "isActive": false}}, {"model": "index.author", "pk": 5, "fields": {"name": "lao wei", "age": 18, "email": "123@qq.com", "isActive": true}}, {"model": "index.author", "pk": 6, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 7, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 8, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 9, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 10, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 11, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 12, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 13, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}, {"model": "index.author", "pk": 14, "fields": {"name": "WangDB", "age": 36, "email": "wangdb@163.com", "isActive": false}}]

首先,从djang.core导入它,然后调用它的serialize方法,这个方法至少接收两个参数,第一个是你要序列化成为的数据格式,这里是‘xml',第二个是要序列化的数据对象,数据通常是ORM模型的QuerySet,一个可迭代的对象。

就是这么简单!!

序列化指定字段

如果你不想序列化模型对象所有字段的内容,只想序列化某些指定的字段,可以使用fields参数,如下所示:

from django.core import serializers
data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size'))
data = serializers.serialize("json", Author.objects.all(),fields=('name','age'))
print(data)

[{"model": "index.author", "pk": 1, "fields": {"name": "pfeiliu", "age": 19}}, {"model": "index.author", "pk": 5, "fields": {"name": "lao wei", "age": 18}}, {"model": "index.author", "pk": 6, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 7, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 8, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 9, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 10, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 11, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 12, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 13, "fields": {"name": "WangDB", "age": 36}}, {"model": "index.author", "pk": 14, "fields": {"name": "WangDB", "age": 36}}]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

六个窍门助你提高Python运行效率

不喜欢Python的人经常会吐嘈Python运行太慢。但是,事实并非如此。尝试以下六个窍门,来为你的Python应用提速。 窍门一:关键代码使用外部功能包 Python简化了许多编程任务...

Python语言描述最大连续子序列和

求最大连续子序列的和是一个很经典很古老的面试题了,记得在刚毕业找工作面试那会也遇到过同款问题。今儿突然想起来,正好快到毕业季,又该是苦逼的应届生们各种面试的时候到了,就给写了一些小代码解...

Python实现计算最小编辑距离

Python实现计算最小编辑距离

最小编辑距离或莱文斯坦距离(Levenshtein),指由字符串A转化为字符串B的最小编辑次数。允许的编辑操作有:删除,插入,替换。具体内容可参见:维基百科—莱文斯坦距离。一般代码实现的...

python处理按钮消息的实例详解

python处理按钮消息的实例详解

python处理按钮消息的实例详解            最新学习Python的基础知...

python中list常用操作实例详解

本文实例讲述了python中list常用操作。分享给大家供大家参考。具体分析如下: 1.定义list >>> li = ["a", "b", "mpilgrim",...