这篇文章主要介绍了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}}]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]