您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“django序列化serializers的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“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}}]
以上是“django序列化serializers的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。