API(一)之Serialization
virtualenv
is a tool to create isolated Python environments.
建立一个新的环境
Before we do anything else we'll create a new virtual environment, using virtualenv. This will make sure our package configuration is kept nicely isolated from any other projects we're working on.
#操作virtualenv ubuntu@ubuntu:~/tutorial$ sudo pip install virtualenv
ubuntu@ubuntu:~/tutorial$ virtualenv env
Running virtualenv with interpreter /usr/bin/python2 #这里默认启动py2,要想启动py3需要切换到py3的目录
错误... ubuntu@ubuntu:~/tutorial$ cd /usr/lib/python3/dist-packages
ubuntu@ubuntu:/usr/lib/python3/dist-packages$ ls env
bin lib local pip-selfcheck.json share
ubuntu@ubuntu:/usr/lib/python3/dist-packages$ cd env/bin
ubuntu@ubuntu:/usr/lib/python3/dist-packages/env/bin$ source activate #激活virtualenv
(env) ubuntu@ubuntu:~$ deactivate #释放virtualenv
ubuntu@ubuntu:~$
# 激活virtualenv virtualenv env
source env/bin/activate
Now that we're inside a virtualenv environment, we can install our package requirements.
# 安装所需模块 pip3 install django
pip3 install djangorestframework
pip3 install pygments # We'll be using this for the code highlighting
Note: To exit the virtualenv environment at any time, just 键入deactivate
. For more information see the virtualenv documentation.
入门
摘要:入门部分创建了一个Django项目。
Okay, we're ready to get coding. To get started, let's create a new project to work with.
# Linux下创建一个项目 cd ~
django-admin.py startproject tutorial
cd tutorial #结果:
ubuntu@ubuntu:~$ tree tutorial
tutorial
├── manage.py
└── tutorial
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py 1 directory, 5 files
Once that's done we can create an app that we'll use to create a simple Web API.
# 创建一个app python3 manage.py startapp snippets
#结果:
ubuntu@ubuntu:~$ tree tutorial
tutorial
├── manage.py
├── snippets
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── tutorial
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ └── settings.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py 4 directories, 14 files
We'll need to add our new snippets
app and the rest_framework
app to INSTALLED_APPS
. Let's edit the tutorial/settings.py
file:
# 配置文件 INSTALLED_APPS = (
...
'snipppets',
'rest_framework',
)
Please note that if you're using Django <1.9, you need to replace snippets.apps.SnippetsConfig
with snippets
.
Okay, we're ready to roll.
创建一个可以使用的model
For the purposes of this tutorial we're going to start by creating a simple Snippet
model that is used to store code snippets. Go ahead and edit the snippets/models.py
file. Note: Good programming practices include comments. Although you will find them in our repository version of this tutorial code, we have omitted them here to focus on the code itself.
# models.py from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles()) class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.TextField()
linenos = models.BooleanField(default=False)
language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100) class Meta:
ordering = ('created',) #排序字段
We'll also need to create an initial migration for our snippet model, and sync the database for the first time.
# 初始化数据库 python3 manage.py makemigrations snippets
python3 manage.py migrate
(不用)创建一个Serializer类
The first thing we need to get started on our Web API is to provide a way of 序列化和反序列化 the snippet instances into representations such as json
. We can do this by declaring serializers that work very similar to Django's forms. Create a file in the snippets
directory named serializers.py
and add the following.
#serializers.py from rest_framework import serializers
from snippets.models import Snippet, LANGUAGE_CHOICES, STYLE_CHOICES class SnippetSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
title = serializers.CharField(required=False, allow_blank=True, max_length=100)
code = serializers.CharField(style={'base_template': 'textarea.html'})
linenos = serializers.BooleanField(required=False)
language = serializers.ChoiceField(choices=LANGUAGE_CHOICES, default='python')
style = serializers.ChoiceField(choices=STYLE_CHOICES, default='friendly') def create(self, validated_data):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
return Snippet.objects.create(**validated_data) def update(self, instance, validated_data):
"""
Update and return an existing `Snippet` instance, given the validated data.
"""
instance.title = validated_data.get('title', instance.title)
instance.code = validated_data.get('code', instance.code)
instance.linenos = validated_data.get('linenos', instance.linenos)
instance.language = validated_data.get('language', instance.language)
instance.style = validated_data.get('style', instance.style)
instance.save()
return instance
the serializer class 的第一部分定义了序列化/反序列化的字段。The create()
and update()
methods define how fully fledged instances are created or modified when calling serializer.save()
serializer class 与 Django的 Form
class 非常相似, 并且包含类似的验证标记, such as required
, max_length
and default
.
在某些情况下,the field flags还可以控制serializer的显示方式,比如渲染html页面. 上面的 {'base_template': 'textarea.html'}
就相当于Django-Form 中的 widget=widgets.Textarea
. 这对于控制可浏览API的显示方式特别有用,我们将在本教程的后面看到。
实际上,我们也可以使用 the ModelSerializer
class 来节省一些时间 , 但是现在我们将保留这种serializer定义。
使用Serializers串行器
摘要:这一部分都是在python shell下操作的,进行序列化和反系列化操作。
在我们进一步了解之前,让我们熟悉一下Serializer类的使用。让我们进入Django shell。
# python shell下 python manage.py shell
好的,几个导入之后,让我们创建一些代码片段来处理。
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser snippet = Snippet(code='foo = "bar"\n') #实例化一个对象并填写code字段,然后保存。
snippet.save() snippet = Snippet(code='print "hello, world"\n')
snippet.save()
现在我们已经有了几个片段实例。我们来序列化其中一个实例。
serializer = SnippetSerializer(snippet) #snippet是Snippet class的对象
serializer.data # {'id': 2, 'title': u'', 'code': u'print "hello, world"\n', 'linenos': False, 'language': u'python', 'style': u'friendly'} type(serializer.data) #<class 'rest_framework.utils.serializer_helpers.ReturnDict'>,不是python原生数据类型。
此时,我们将model实例转换为Python原生数据类型。为了完成序列化过程,我们先将数据转化为json
。
content = JSONRenderer().render(serializer.data)
content # b'{"id": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}' type(content) #<class 'bytes'>
反序列化是类似的. 首先我们将一个流解析为Python原生数据类型...
from django.utils.six import BytesIO stream = BytesIO(content) type(stream) #<class '_io.BytesIO'>
data = JSONParser().parse(stream) type(data) #<class 'dict'>,python原生数据类型。
小结:
实例化:snippet = Snippet(code='foo = "bar"\n')
序列化:serializer = SnippetSerializer(snippet)
因为serializer.data不是python原生数据类型(<class 'rest_framework.utils.serializer_helpers.ReturnDict'>)
要想转为python原生数据类型需要两个中间步骤:
转为json:content = JSONRenderer().render(serializer.data)
转为BytesIO:stream = BytesIO(content)
转为原生数据类型:data = JSONParser().parse(stream)
...然后我们将这些原生数据类型恢复到 a fully populated object instance。
serializer = SnippetSerializer(data=data) #data是python原生数据类型。
serializer.is_valid()
# True
serializer.validated_data
# OrderedDict([('title', ''), ('code', 'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])
serializer.save()
# <Snippet: Snippet object>
请注意,API 与 forms 很相似。当我们使用serializer编写视图函数时,相似性会更加明显。
我们也可以序列化 querysets 而不是 model instances. 为了做到这一点, 我们仅仅添加 a many=True
flag to the serializer arguments.
serializer = SnippetSerializer(Snippet.objects.all(), many=True)
serializer.data
# [OrderedDict([('id', 1), ('title', u''), ('code', u'foo = "bar"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 2), ('title', u''), ('code', u'print "hello, world"\n'), ('linenos', False), ('language', 'python'), ('style', 'friendly')]), OrderedDict([('id', 3), ('title', u''), ('code', u'print "hello, world"'), ('linenos', False), ('language', 'python'), ('style', 'friendly')])]
(使用)使用ModelSerializers
SnippetSerializer
复制了Snippet
中的大量信息。如果可以保持代码更简洁,那将是很好的。
与Django提供Form
类和ModelForm
类的方式相同,REST框架包括Serializer
类和ModelSerializer
类。
让我们来看看使用ModelSerializer
类重构我们的serializer。再次打开文件 snippets/serializers.py
, and replace the SnippetSerializer
class with the following.
from snippets import models
from rest_framework import serializers class SnippetSerializer(serializers.ModelSerializer): #之前继承的是serializers.Serializer
class Meta:
model = models.Snippet
fields = ('id', 'title', 'code', 'linenos', 'language', 'style')
serializers有一个很好的属性是你可以检查一个serializer instance的所有字段, by printing its representation. 打开Django shell python manage.py shell
,然后尝试以下操作:
from snippets.serializers import SnippetSerializer
serializer = SnippetSerializer()
print(repr(serializer))
# SnippetSerializer():
# id = IntegerField(label='ID', read_only=True)
# title = CharField(allow_blank=True, max_length=100, required=False)
# code = CharField(style={'base_template': 'textarea.html'})
# linenos = BooleanField(required=False)
# language = ChoiceField(choices=[('Clipper', 'FoxPro'), ('Cucumber', 'Gherkin'), ('RobotFramework', 'RobotFramework'), ('abap', 'ABAP'), ('ada', 'Ada')...
# style = ChoiceField(choices=[('autumn', 'autumn'), ('borland', 'borland'), ('bw', 'bw'), ('colorful', 'colorful')...
记住,ModelSerializer
类不会做任何特别神奇的事情,它们只是创建 serializer classes 快捷方式:
- 自动确定具有的字段。
- 默认实现
create()
和update()
方法。
(不用,在第二篇加了format)使用Serializer编写常规的Django视图
让我们来看看该如何使用Serializer类来编写一些API视图。目前我们不会使用任何REST框架的其他功能,我们只编写一些常规的Django视图函数。
编辑 snippets/views.py
文件,并添加以下内容。
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
The root of our API 将是一个视图,它支持列出所有现有的片段,或创建一个新的片段。
@csrf_exempt
def snippet_list(request):
"""
List all code snippets, or create a new snippet.
"""
if request.method == 'GET':
snippets = Snippet.objects.all() #querysets
serializer = SnippetSerializer(snippets, many=True) #序列化querysets,many=True
return JsonResponse(serializer.data, safe=False) #safe=False,是啥??? elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = SnippetSerializer(data=data) #SnippetSerializer中有data???
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
请注意,因为我们希望能够从不具有CSRF token的客户端以POST方法访问此视图函数,因此我们需要用csrf_exempt
标记视图。这不是你通常想要做的事情,REST框架视图实际上使用比这更有明显的行为,但它现在将用于我们的目的。
我们还需要一个与 an individual snippet对应的视图,并可用于检索,更新或删除the snippet。
@csrf_exempt
def snippet_detail(request, pk):
"""
Retrieve, update or delete a code snippet.
"""
try:
snippet = Snippet.objects.get(pk=pk) #pk???
except Snippet.DoesNotExist:
return HttpResponse(status=404) if request.method == 'GET':
serializer = SnippetSerializer(snippet)
return JsonResponse(serializer.data) elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = SnippetSerializer(snippet, data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data)
return JsonResponse(serializer.errors, status=400) elif request.method == 'DELETE':
snippet.delete()
return HttpResponse(status=204)
最后我们需要把这些视图函数连接起来。创建snippets/urls.py
文件:
#app下面的urls.py from django.conf.urls import url
from snippets import views urlpatterns = [
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
]
我们还需要在tutorial/urls.py
文件中连接根urlconf ,以包含我们的片段应用程序的URL。
from django.conf.urls import url, include urlpatterns = [
url(r'^', include('snippets.urls')),
]
It's worth noting that there are a couple of edge cases we're not dealing with properly at the moment. If we send malformed json
, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.
测试我们在Web API上的第一次尝试
现在我们可以启动一个运行我们的代码片段的示例服务器。
退出shell...
quit()
...and start up Django's development server.
python manage.py runserver 192.168.66.250:9000 Validating models... 0 errors found
Django version 1.11, using settings 'tutorial.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Notes:在settings.py中添加ALLOWED_HOSTS = {'192.168.66.200',}
在另一个终端窗口中,我们可以测试服务器。
我们可以使用curl或httpie来测试我们的API 。Httpie是用Python编写的用户友好的http客户端。我们来安装它。
您可以使用pip安装httpie:
# pip install httpie
最后,我们可以得到所有片段的列表:
# http http://127.0.0.1:8000/snippets/ HTTP/1.1 200 OK
...
[
{
"id": 1,
"title": "",
"code": "foo = \"bar\"\n",
"linenos": false,
"language": "python",
"style": "friendly"
},
{
"id": 2,
"title": "",
"code": "print \"hello, world\"\n",
"linenos": false,
"language": "python",
"style": "friendly"
}
]
或者我们可以通过引用其id来获取特定的代码段:
# http http://127.0.0.1:8000/snippets/2/ HTTP/1.1 200 OK
...
{
"id": 2,
"title": "",
"code": "print \"hello, world\"\n",
"linenos": false,
"language": "python",
"style": "friendly"
}
同样,您可以通过在网络浏览器中访问这些URL来显示相同的json。
API(一)之Serialization的更多相关文章
- Jersey(1.19.1) - Client API, Ease of use and reusing JAX-RS artifacts
Since a resource is represented as a Java type it makes it easy to configure, pass around and inject ...
- 使用 Node.js 搭建API 网关
外部客户端访问微服务架构中的服务时,服务端会对认证和传输有一些常见的要求.API 网关提供共享层来处理服务协议之间的差异,并满足特定客户端(如桌面浏览器.移动设备和老系统)的要求. 微服务和消费者 微 ...
- .Net 读取配置文件 xml
直接解析XML文件 1.System.Xml.Linq命名空间下提供可以使用linq查询的类,使用linq to xml读取也很方便. 2.还可以使用System.Xml.Serialization类 ...
- 【资源大全】.NET资源大全中文版(Awesome最新版)
算法与数据结构(Algorithms and Data structures) 应用程序接口(API) 应用程序框架(Application Frameworks) 模板引擎(Application ...
- 使用示例带你提前了解 Java 9 中的新特性
使用示例带你提前了解 Java 9 中的新特性 转载来源:https://juejin.im/post/58c5e402128fe100603cc194 英文出处:https://www.journa ...
- [翻译] Working with NSURLSession: AFNetworking 2.0
Working with NSURLSession: AFNetworking 2.0 简单翻译,有很多错误,看官无法理解处请英文原文对照. http://code.tutsplus.com/tu ...
- Laravel手记:执行route:cache时报LogicException
laravel5的路由支持缓存.需要执行以下命令: php artisan route:cache 执行完毕后,报出以下错误: Route cache cleared! [LogicException ...
- 大数据查询——HBase读写设计与实践--转
背景介绍 本项目主要解决 check 和 opinion2 张历史数据表(历史数据是指当业务发生过程中的完整中间流程和结果数据)的在线查询.原实现基于 Oracle 提供存储查询服务,随着数据量的不断 ...
- Avro使用手册
1. Overview Data serialization is a technique of converting data into binary or text format. There a ...
随机推荐
- 【Matplotlib】数据可视化实例分析
数据可视化实例分析 作者:白宁超 2017年7月19日09:09:07 摘要:数据可视化主要旨在借助于图形化手段,清晰有效地传达与沟通信息.但是,这并不就意味着数据可视化就一定因为要实现其功能用途而令 ...
- 【CLR】解析CLR的托管堆和垃圾回收
目录结构: contents structure [+] 为什么使用托管堆 从托管堆中分配资源 托管堆中的垃圾回收 垃圾回收算法 代 垃圾回收模式 垃圾回收触发条件 强制垃圾回收 监视内存 对包装了本 ...
- Java list对象列表排序 实例
package com.test; public class Bean { private String name; private int priority; public String getNa ...
- css + div 列表布局
常见列表布局,效果如下图.常见图与图之间经常会留间距,下图图与图没留间距 1.第一种列表布局:float + margin 1.2.第一种列表布局相应代码 <!DOCTYPE html> ...
- 第三部分:Android 应用程序接口指南---第三节:应用程序资源---第一章 资源提供
第1章 资源提供 你应该经常外部化你应用程序代码中的资源,比如图片.字符串等,这样有利于你独立处理这些资源.你也应该根据特定的设备配置提供一些可替代的资源,并且把他们分组保存在指定的路径名下.运行时, ...
- Android studio的主题颜色修改
1.选择喜欢的主题 http://color-themes.com/?view=index 好几十款,总有一款你喜欢 2.下载你喜欢的主题,注意是jar文件 .File -> Import Se ...
- 清除win下连接的linux的samba服务缓存 用户名和密码
1:cmd 2:在停止查看共享的情况下执行:net use * /del 删除所有 或根据列表,一个个删除连接: net use 远程连接名称 /del
- linux每日命令(23):find命令之xargs
在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...
- 【emWin】例程二十七:窗口对象——Listview
简介: LISTVIEW小工具可在具有多个列的列表中选择某个元素.由于LISTVIEW小工具包含了 一个HEADER小工具,因此可对列加以管理(排序等).所创建的LISTVIEW 既可以无环绕型框架窗 ...
- ambari安装 QA
1.在安装时 出现Public key for ambari-server-2.4.2.0-136.x86_64.rpm is not installed 安装ambari报错 在安装HST服务时也报 ...