django rest framework 入门1-序列化 Serialization

转自:http://blog.csdn.net/zhaoyingm/article/details/8531617

1. 设置一个新的环境

在我们开始之前, 我们首先使用virtualenv要创建一个新的虚拟环境,以使我们的配置和我们的其他项目配置彻底分开。

  $mkdir ~/env

$virtualenv  ~/env/tutorial

$source ~/env/tutorial/bin/avtivate

现在我们处在一个虚拟的环境中,开始安装我们的依赖包

$pip install django

$pip install djangorestframework

$pip install pygments   使用这个包,做代码高亮显示


需要退出虚拟环境时,运行deactivate。更多信息,irtualenv document


2. 开始

环境准备好只好,我们开始创建我们的项目

$ cd ~

$ django-admin.py startproject tutorial

$ cd tutorial

项目创建好后,我们再创建一个简单的app

$python manage.py startapp snippets

我们使用sqlite3来运行我们的项目tutorial,编辑tutorial/settings.py, 将数据库的默认引擎engine改为sqlite3, 数据库的名字NAME改为tmp.db

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">DATABASES </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">{</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">'default'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">{</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'ENGINE'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'django.db.backends.sqlite3'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'NAME'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'tmp.db'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'USER'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'PASSWORD'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'HOST'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">'PORT'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(147, 161, 161);">}</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(147, 161, 161);">}</span></code>
同时更改settings.py文件中的INSTALLD_APPS,添加我们的APP snippets和rest_framework

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">INSTALLED_APPS </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(147, 161, 161);">...</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">'rest_framework'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">'snippets'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(147, 161, 161);">)</span></code>
在tutorial/urls.py中,将snippets app的url包含进来

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">urlpatterns </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> patterns</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
    url</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">r</span><span style="color: rgb(221, 17, 68);">'^'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> include</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(221, 17, 68);">'snippets.urls'</span><span style="color: rgb(147, 161, 161);">)),</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(147, 161, 161);">)</span></code>
3. 创建Model

这里我们创建一个简单的nippets model,目的是用来存储代码片段。

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> django</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">db </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> models
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> pygments</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">lexers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> get_all_lexers
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> pygments</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">styles </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> get_all_styles

LEXERS </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(72, 72, 76);">item </span><span style="color: rgb(30, 52, 123);">for</span><span style="color: rgb(72, 72, 76);"> item </span><span style="color: rgb(30, 52, 123);">in</span><span style="color: rgb(72, 72, 76);"> get_all_lexers</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> item</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(25, 95, 145);">1</span><span style="color: rgb(147, 161, 161);">]]</span><span style="color: rgb(72, 72, 76);">
LANGUAGE_CHOICES </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> sorted</span><span style="color: rgb(147, 161, 161);">([(</span><span style="color: rgb(72, 72, 76);">item</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(25, 95, 145);">1</span><span style="color: rgb(147, 161, 161);">][</span><span style="color: rgb(25, 95, 145);">0</span><span style="color: rgb(147, 161, 161);">],</span><span style="color: rgb(72, 72, 76);"> item</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(25, 95, 145);">0</span><span style="color: rgb(147, 161, 161);">])</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(30, 52, 123);">for</span><span style="color: rgb(72, 72, 76);"> item </span><span style="color: rgb(30, 52, 123);">in</span><span style="color: rgb(72, 72, 76);"> LEXERS</span><span style="color: rgb(147, 161, 161);">])</span><span style="color: rgb(72, 72, 76);">
STYLE_CHOICES </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> sorted</span><span style="color: rgb(147, 161, 161);">((</span><span style="color: rgb(72, 72, 76);">item</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> item</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(30, 52, 123);">for</span><span style="color: rgb(72, 72, 76);"> item </span><span style="color: rgb(30, 52, 123);">in</span><span style="color: rgb(72, 72, 76);"> get_all_styles</span><span style="color: rgb(147, 161, 161);">())</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">Model</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    created </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">DateTimeField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">auto_now_add</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(30, 52, 123);">True</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    title </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">CharField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">max_length</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">100</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">''</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    code </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">TextField</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
    linenos </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">BooleanField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(30, 52, 123);">False</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    language </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">CharField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">choices</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">LANGUAGE_CHOICES</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">'python'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                max_length</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">100</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    style </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">CharField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">choices</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">STYLE_CHOICES</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                             default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">'friendly'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                             max_length</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">100</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Meta</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        ordering </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(221, 17, 68);">'created'</span><span style="color: rgb(147, 161, 161);">,)</span></code>
完成model时,记得sync下数据库

python manage.py syncdb


4. 创建序列化类

我们要使用我们的web api,要做的第一件事就是序列化和反序列化, 以便snippets实例能转换为可表述的内容,例如json. 我们声明一个可有效工作的串行器serializer。在snippets目录下面,该串行器与django 的表单形式很类似。创建一个serializers.py ,并将下面内容拷贝到文件中。

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> django</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">forms </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> widgets
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> rest_framework </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> serializers
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> snippets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">models </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">Serializer</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    pk </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">Field</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">  </span><span style="color: rgb(147, 161, 161);"># Note: `Field` is an untyped read-only field.</span><span style="color: rgb(72, 72, 76);">
    title </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">CharField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">required</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(30, 52, 123);">False</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                  max_length</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">100</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    code </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">CharField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">widget</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">widgets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">Textarea</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                 max_length</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">100000</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    linenos </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">BooleanField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">required</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(30, 52, 123);">False</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    language </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">ChoiceField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">choices</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">LANGUAGE_CHOICES</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                       default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">'python'</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    style </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">ChoiceField</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">choices</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">models</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">STYLE_CHOICES</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
                                    default</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">'friendly'</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">def</span><span style="color: rgb(72, 72, 76);"> restore_object</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">self</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> instance</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(30, 52, 123);">None</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(221, 17, 68);">"""
        Create or update a new snippet instance.
        """</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> instance</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(147, 161, 161);"># Update existing instance</span><span style="color: rgb(72, 72, 76);">
            instance</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">title </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'title'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);">
            instance</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">code </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'code'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);">
            instance</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">linenos </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'linenos'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);">
            instance</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">language </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'language'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);">
            instance</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">style </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> attrs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'style'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> instance

        </span><span style="color: rgb(147, 161, 161);"># Create new instance</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">(**</span><span style="color: rgb(72, 72, 76);">attrs</span><span style="color: rgb(147, 161, 161);">)</span></code>

该序列化类的前面部分,定义了要序列化和反序列化的类型,restore_object 方法定义了如何通过反序列化数据,生成正确的对象实例。

我们也可以使用ModelSerializer来快速生成,后面我们将节省如何使用它。


5. 使用 Serializers

在我们使用我们定义的SnippetsSerializers之前,我们先熟悉下Snippets. 

 $python manage.py shell

进入shell终端后,输入以下代码:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> snippets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">models </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> snippets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">serializers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> rest_framework</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">renderers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONRenderer</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> rest_framework</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">parsers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONParser</span><span style="color: rgb(72, 72, 76);">

snippet </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">code</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(221, 17, 68);">'print "hello, world"\n'</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
snippet</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">save</span><span style="color: rgb(147, 161, 161);">()</span></code>
我们现在获得了一个Snippets的实例,现在我们对他进行以下序列化

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">snippet</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data
</span><span style="color: rgb(147, 161, 161);"># {'pk': 1, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'}</span></code>
这时,我们将该实例转成了python原生的数据类型。下面我们将该数据转换成json格式,以完成序列化:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">content </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONRenderer</span><span style="color: rgb(147, 161, 161);">().</span><span style="color: rgb(72, 72, 76);">render</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
content
</span><span style="color: rgb(147, 161, 161);"># '{"pk": 1, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'</span></code>
反序列化也很简单,首先我们要将一个输入流(content),转换成python的原生数据类型

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">StringIO</span><span style="color: rgb(72, 72, 76);">

stream </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">StringIO</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">StringIO</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">content</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
data </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONParser</span><span style="color: rgb(147, 161, 161);">().</span><span style="color: rgb(72, 72, 76);">parse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">stream</span><span style="color: rgb(147, 161, 161);">)</span></code>
然后我们将该原生数据类型,转换成对象实例

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">is_valid</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(147, 161, 161);"># True</span><span style="color: rgb(72, 72, 76);">
serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">object
</span><span style="color: rgb(147, 161, 161);"># <Snippet: Snippet object></span></code>
注意这些API和django表单的相似处。这些相似点, 在我们讲述在view中使用serializers时将更加明显。


6. 使用 ModelSerializers

SnippetSerializer使用了许多和Snippet中相同的代码。如果我们能把这部分代码去掉,看上去将更佳简洁。

类似与django提供Form类和ModelForm类,Rest Framework也包含了Serializer 类和 ModelSerializer类。

打开snippets/serializers.py ,修改SnippetSerializer类:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializers</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">ModelSerializer</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Meta</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        model </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(72, 72, 76);">
        fields </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(221, 17, 68);">'id'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'title'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'code'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'linenos'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'language'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'style'</span><span style="color: rgb(147, 161, 161);">)</span></code>

7. 通过Serializer编写Django View

让我们来看一下,如何通过我们创建的serializer类编写django view。这里我们不使用rest framework的其他特性,仅编写正常的django view。

我们创建一个HttpResponse 子类,这样我们可以将我们返回的任何数据转换成json。

在snippet/views.py中添加以下内容:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> django</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">http </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">HttpResponse</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> django</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">views</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">decorators</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">csrf </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> csrf_exempt
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> rest_framework</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">renderers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONRenderer</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> rest_framework</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">parsers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONParser</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> snippets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">models </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">from</span><span style="color: rgb(72, 72, 76);"> snippets</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">serializers </span><span style="color: rgb(30, 52, 123);">import</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(30, 52, 123);">class</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: teal;">HttpResponse</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">"""
    An HttpResponse that renders it's content into JSON.
    """</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(30, 52, 123);">def</span><span style="color: rgb(72, 72, 76);"> __init__</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">self</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> data</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">**</span><span style="color: rgb(72, 72, 76);">kwargs</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
        content </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONRenderer</span><span style="color: rgb(147, 161, 161);">().</span><span style="color: rgb(72, 72, 76);">render</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        kwargs</span><span style="color: rgb(147, 161, 161);">[</span><span style="color: rgb(221, 17, 68);">'content_type'</span><span style="color: rgb(147, 161, 161);">]</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'application/json'</span><span style="color: rgb(72, 72, 76);">
        super</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> self</span><span style="color: rgb(147, 161, 161);">).</span><span style="color: rgb(72, 72, 76);">__init__</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">content</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(147, 161, 161);">**</span><span style="color: rgb(72, 72, 76);">kwargs</span><span style="color: rgb(147, 161, 161);">)</span></code>
我们API的目的是,可以通过view来列举全部的Snippet的内容,或者创建一个新的snippet

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(25, 95, 145);">@csrf_exempt</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">def</span><span style="color: rgb(72, 72, 76);"> snippet_list</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">request</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">"""
    List all code snippets, or create a new snippet.
    """</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> request</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">method </span><span style="color: rgb(147, 161, 161);">==</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'GET'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        snippets </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">objects</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">all</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
        serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">snippets</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">elif</span><span style="color: rgb(72, 72, 76);"> request</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">method </span><span style="color: rgb(147, 161, 161);">==</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'POST'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        data </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONParser</span><span style="color: rgb(147, 161, 161);">().</span><span style="color: rgb(72, 72, 76);">parse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">request</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">is_valid</span><span style="color: rgb(147, 161, 161);">():</span><span style="color: rgb(72, 72, 76);">
            serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">save</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> status</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">201</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">else</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">errors</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> status</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">400</span><span style="color: rgb(147, 161, 161);">)</span></code>
注意,因为我们要通过client向该view post一个请求,所以我们要将该view 标注为csrf_exempt, 以说明不是一个CSRF事件。

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.


我们也需要一个view来操作一个单独的Snippet,以便能更新/删除该对象。

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(25, 95, 145);">@csrf_exempt</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(30, 52, 123);">def</span><span style="color: rgb(72, 72, 76);"> snippet_detail</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">request</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> pk</span><span style="color: rgb(147, 161, 161);">):</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(221, 17, 68);">"""
    Retrieve, update or delete a code snippet.
    """</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(30, 52, 123);">try</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        snippet </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">objects</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">get</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">pk</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">pk</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
    </span><span style="color: rgb(30, 52, 123);">except</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">Snippet</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: teal;">DoesNotExist</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">HttpResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">status</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">404</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> request</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">method </span><span style="color: rgb(147, 161, 161);">==</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'GET'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">snippet</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">elif</span><span style="color: rgb(72, 72, 76);"> request</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">method </span><span style="color: rgb(147, 161, 161);">==</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'PUT'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        data </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONParser</span><span style="color: rgb(147, 161, 161);">().</span><span style="color: rgb(72, 72, 76);">parse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">request</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        serializer </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">SnippetSerializer</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">snippet</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> data</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">if</span><span style="color: rgb(72, 72, 76);"> serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">is_valid</span><span style="color: rgb(147, 161, 161);">():</span><span style="color: rgb(72, 72, 76);">
            serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">save</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">data</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">else</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
            </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">JSONResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">serializer</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">errors</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> status</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">400</span><span style="color: rgb(147, 161, 161);">)</span><span style="color: rgb(72, 72, 76);">

    </span><span style="color: rgb(30, 52, 123);">elif</span><span style="color: rgb(72, 72, 76);"> request</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">method </span><span style="color: rgb(147, 161, 161);">==</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'DELETE'</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);">
        snippet</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">delete</span><span style="color: rgb(147, 161, 161);">()</span><span style="color: rgb(72, 72, 76);">
        </span><span style="color: rgb(30, 52, 123);">return</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: teal;">HttpResponse</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">status</span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(25, 95, 145);">204</span><span style="color: rgb(147, 161, 161);">)</span></code>
将views.py保存,在Snippets目录下面创建urls.py,添加以下内容:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">urlpatterns </span><span style="color: rgb(147, 161, 161);">=</span><span style="color: rgb(72, 72, 76);"> patterns</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(221, 17, 68);">'snippets.views'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);">
    url</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">r</span><span style="color: rgb(221, 17, 68);">'^snippets/$'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'snippet_list'</span><span style="color: rgb(147, 161, 161);">),</span><span style="color: rgb(72, 72, 76);">
    url</span><span style="color: rgb(147, 161, 161);">(</span><span style="color: rgb(72, 72, 76);">r</span><span style="color: rgb(221, 17, 68);">'^snippets/(?P<pk>[0-9]+)/$'</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">'snippet_detail'</span><span style="color: rgb(147, 161, 161);">),</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: rgb(147, 161, 161);">)</span></code>
注意我们有些边缘事件没有处理,服务器可能会抛出500异常。


8. 测试

现在我们启动server来测试我们的Snippet。

在python mange.py shell终端下执行(如果前面进入还没有退出)

  >>quit()


执行下面的命令, 运行我们的server:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">python manage</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(72, 72, 76);">py runserver

</span><span style="color: teal;">Validating</span><span style="color: rgb(72, 72, 76);"> models</span><span style="color: rgb(147, 161, 161);">...</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(25, 95, 145);">0</span><span style="color: rgb(72, 72, 76);"> errors found
</span><span style="color: teal;">Django</span><span style="color: rgb(72, 72, 76);"> version </span><span style="color: rgb(25, 95, 145);">1.4</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(25, 95, 145);">3</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> using settings </span><span style="color: rgb(221, 17, 68);">'tutorial.settings'</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: teal;">Development</span><span style="color: rgb(72, 72, 76);"> server </span><span style="color: rgb(30, 52, 123);">is</span><span style="color: rgb(72, 72, 76);"> running at http</span><span style="color: rgb(147, 161, 161);">://</span><span style="color: rgb(25, 95, 145);">127.0</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(25, 95, 145);">0.1</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(25, 95, 145);">8000</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(72, 72, 76);">
</span><span style="color: teal;">Quit</span><span style="color: rgb(72, 72, 76);"> the server </span><span style="color: rgb(30, 52, 123);">with</span><span style="color: rgb(72, 72, 76);"> CONTROL</span><span style="color: rgb(147, 161, 161);">-</span><span style="color: rgb(72, 72, 76);">C</span><span style="color: rgb(147, 161, 161);">.</span></code>

新开一个terminal来测试我们的server

序列化:

<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">url http</span><span style="color: rgb(147, 161, 161);">://</span><span style="color: rgb(25, 95, 145);">127.0</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(25, 95, 145);">0.1</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(25, 95, 145);">8000</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(72, 72, 76);">snippets</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(147, 161, 161);">[{</span><span style="color: rgb(221, 17, 68);">"id"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(25, 95, 145);">1</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"title"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">""</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"code"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"print \"hello, world\"\n"</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"linenos"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> false</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"language"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"python"</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"style"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"friendly"</span><span style="color: rgb(147, 161, 161);">}]</span></code>
<code style="padding: 0px; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: inherit; border: 0px; background-color: transparent;"><span style="color: rgb(72, 72, 76);">url http</span><span style="color: rgb(147, 161, 161);">://</span><span style="color: rgb(25, 95, 145);">127.0</span><span style="color: rgb(147, 161, 161);">.</span><span style="color: rgb(25, 95, 145);">0.1</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(25, 95, 145);">8000</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(72, 72, 76);">snippets</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(25, 95, 145);">1</span><span style="color: rgb(147, 161, 161);">/</span><span style="color: rgb(72, 72, 76);">

</span><span style="color: rgb(147, 161, 161);">{</span><span style="color: rgb(221, 17, 68);">"id"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(25, 95, 145);">1</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"title"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">""</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"code"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"print \"hello, world\"\n"</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"linenos"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> false</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"language"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"python"</span><span style="color: rgb(147, 161, 161);">,</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"style"</span><span style="color: rgb(147, 161, 161);">:</span><span style="color: rgb(72, 72, 76);"> </span><span style="color: rgb(221, 17, 68);">"friendly"</span><span style="color: rgb(147, 161, 161);">}</span></code>

来源:

http://django-rest-framework.org/tutorial/1-serialization.html#tutorial-1-serialization

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值