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 ...
随机推荐
- 我是陌生人 Java中导入、导出Excel
我是陌生人 Java中导入.导出Excel 一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是: ...
- OpenCV 学习笔记 04 深度估计与分割
本章节主要是使用深度摄像头的数据来识别前景区和背景区,这样就可以分别对前景和背景做不同的处理. 1 创建模块
- [na][dhcp]dhcp细枝末节&dhcp防攻
回顾了下,真是以前是一种感觉以后是一种感觉. 特点: 1.dhcp服务器上的配置的网关不一定要有这个ip 2.dhcp服务只是个类似数据库而已(如果不在一个lan). 3. 如果dhcp不在一个lan ...
- PNG、 JPG图片压缩方法
参考链接 https://tinypng.com/developers/reference/python 1.安装 pip install --upgrade tinify 2.使用python脚本压 ...
- 把上传过来的多张图片拼接转为PDF的实现代码
以下是把上传过来的多张图片拼接转为PDF的实现代码,不在本地存储上传上来的图片,下面是2中做法,推荐第一种,把pdf直接存储到DB中比较安全. 如果需要在服务器上存储客户端上传的文件时,切记存储文件时 ...
- hdoj:2035
#include <iostream> using namespace std; int main() { long a, b; && b != ) { long resu ...
- Java知多少(90)菜单
有两种类型的菜单:下拉式菜单和弹出式菜单.本章只讨论下拉式菜单编程方法.菜单与JComboBox和JCheckBox不同,它们在界面中是一直可见的.菜单与JComboBox的相同之处是每次只可选择一个 ...
- 安卓程序代写 网上程序代写[原]vim编辑器配置及常用命令
最近工作不安分, 没有了刚入行时候的锐气, 不知道什么时候开始懈怠起来, 周末在电脑旁边看新闻, 搞笑图片, 追美剧, 一坐就是一天, 很是空虚. 我需要摆脱这种状态, 正好想学习一下安卓底层, An ...
- Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization
I know that blog post title is sure a mouth-full, but it describes the whole problem I was trying to ...
- SqlServer 数据去重
找出所有不重复的数据 SELECT [Id] ,[Name] ,[Mobile] ,[City] ,[Counter] ,[utm_source] ,[utm_medium] ,[utm_campai ...