前言:第一次接触django-rest-framework是在实习的时候。当时也不懂,看到视图用类方法写的感受很牛逼的样子。由于官网是英文的,这对个人学习仍是有一点的阻力的,因此当时也没怎么学。真是太贱了。其实官网有耐心,以我六级410(虽然也没过)的能力,确定也是能搞懂的阿。追其缘由,仍是当时本身太浮躁了。java
Django REST framework is a powerful and flexible toolkit for building Web APIs.python
Some reasons you might want to use REST framework:git
The Web browsable API is a huge usability win for your developers.
Authentication policies including packages for OAuth1a and OAuth2.
Serialization that supports both ORM and non-ORM data sources.
Customizable all the way down - just use regular function-based views if you don't need the more powerful features.
Extensive documentation, and great community support.
Used and trusted by internationally recognised companies including Mozilla, Red Hat, Heroku, and Eventbrite.github
中文:
Django REST framework 是用于构建Web API 的强大而灵活的工具包。数据库
咱们可能想使用REST框架的一些缘由:django
Web浏览API对于开发人员来讲是一个巨大的可用性。
认证策略包括OAuth1a和OAuth2的包。
支持ORM和非ORM数据源的序列化。
若是你不须要更强大的功能,就可使用常规的基于功能的视图。
普遍的文档和良好的社区支持。
包括Mozilla、Red Hat、Heroku和Eventbrite在内的国际知名公司使用和信任。api
REST framework is a collaboratively(合做地) funded project(基金项目). If you use REST framework commercially we strongly encourage you to invest(投资) in its continued development(可持续发展) by signing up for a paid plan.(注册付费计划)markdown
Every single sign-up helps us make REST framework long-term financially sustainable(财务上可持续发展)app
Rover.com
Sentry
Stream
Machinalis
Rollbar
Many thanks to all our wonderful sponsors(赞助商), and in particular to our premium backers(优质的支持者), Rover, Sentry, Stream, Machinalis, and Rollbar.框架
REST framework requires the following:
Python (2.7, 3.2, 3.3, 3.4, 3.5, 3.6)
Django (1.10, 1.11, 2.0 alpha)
The following packages are optional:
coreapi (1.32.0+) - Schema generation support.
Markdown (2.1.0+) - Markdown support for the browsable API.
django-filter (1.0.1+) - Filtering support.
django-crispy-forms - Improved HTML display for filtering.
django-guardian (1.1.1+) - Object level permissions support.
如下软件包是可选的:
coreapi(1.32.0+) - 支持模式生成。
Markdown(2.1.0+) - 可浏览API的Markdown支持。
django-filter(1.0.1+) - 过滤支持。
django-crispy-forms - 改进的HTML显示过滤。
django-guardian(1.1.1+) - 对象级权限支持。
Install using pip, including any optional packages you want…
…or clone the project from github.
Add 'rest_framework' to your INSTALLED_APPS setting.(记得在setting文件里面添加rest_framework,固然,你还得先安装djangorestframework)
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root urls.py file.
若是您打算使用可浏览的API,您可能还须要添加REST框架的登陆和注销视图。将如下内容添加到您的根urls.py文件中。
Note that the URL path can be whatever you want, but you must include 'rest_framework.urls' with the 'rest_framework' namespace. You may leave out the namespace in Django 1.9+, and REST framework will set it for you.
请注意,URL路径能够是任何你想要的,但你必须包括'rest_framework.urls'与'rest_framework'命名空间。您能够在Django 1.9+中省略命名空间,REST框架将为您设置。
Can't wait to get started? The quickstart guide is the fastest way to get up and running, and building APIs with REST framework.
说了一堆,直接来个demo,快速上手,看看效果。官网请看:http://www.django-rest-framework.org/tutorial/quickstart/
首先确定得先建立django程序啦,接着建立APP,这里我建立了一个quickstart的app。
Now sync your database for the first time:同步数据库
建立超级用户用于登录。We'll also create an initial user named admin with a password of password123. We'll authenticate as that user later in our example.
首先咱们要定义一些序列化程序。在quickstart这个APP下建立serializers文件,用于展现数据。
First up we're going to define some serializers. Let's create a new module named tutorial/quickstart/serializers.py that we'll use for our data representations.
Notice that we're using hyperlinked relations in this case, with HyperlinkedModelSerializer. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
请注意,在这种状况下,咱们正在使用超连接关系HyperlinkedModelSerializer。您还可使用主键和各类其余关系,但超连接是好的RESTful设计。
Right, we'd better write some views then. Open tutorial/quickstart/views.py and get typing. 写一些视图,查询数据。
Rather than write multiple views we're grouping together all the common behavior into classes called ViewSets.
We can easily break these down into individual views if we need to, but using viewsets keeps the view logic nicely organized as well as being very concise.
咱们不是编写多个视图,而是将全部常见的行为组合到一个名为viewset的类中。
若是须要的话,咱们能够很容易地将它们分解为单独的视图,可是使用viewset使视图逻辑组织得很好,而且很是简洁。
Okay, now let's wire up the API URLs. On to tutorial/urls.py…
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
咱们能够经过简单地使用路由器类注册该视图来自动生成API的URL conf。
Again, if we need more control over the API URLs we can simply drop down to using regular class-based views, and writing the URL conf explicitly.
再次,若是咱们须要对API URL的更多控制,咱们能够简单地将其下拉到使用常规的基于类的视图,并明确地编写URL conf。
Finally, we're including default login and logout views for use with the browsable API. That's optional, but useful if your API requires authentication and you want to use the browsable API.
最后,咱们将包括默认登陆和注销视图,以便与可浏览的API一块儿使用。这是可选的,但若是您的API须要身份验证,而且您想要使用可浏览的API,那么这是很是有用的。
We'd also like to set a few global settings. We'd like to turn on pagination, and we want our API to only be accessible to admin users. The settings module will be in tutorial/settings.py
咱们也想设置一些全局设置。咱们想打开分页,咱们但愿咱们的API只能由管理员使用
Okay, we're done.
主界面,好像啥也没有……
用超级用户登录后的界面。
有增删改查的功能。
快速了解REST framework组件
接下来了解下rest framework 的全部组件,而且得知它们是如何组合在一块儿的,这是很是值得去学习的。
1 - Serialization 序列化
2 - Requests & Responses 请求 & 响应
3 - Class-based views 基于类的视图
4 - Authentication & permissions 身份验证 & 权限
5 - Relationships & hyperlinked APIs 这个貌似还没用过,暂时留着吧,哈哈~
6 - Viewsets & routers 视图和路由
7 - Schemas & client libraries 模式和客户端库(虚位以待~)
Serialization 序列化
这里呢,不对普通的序列化做介绍。接下来咱们使用下 ModelSerializers model序列化让咱们写的代码更少,更简介。Django提供了form和modelform同样,REST框架包括了序列化器类和模型序列化器类。
例如 models 文件中有一个关于文章的表:
接下来,只须要写一个序列化器,即可以轻松对数据的进行获取,并且代码看起来特别简洁。
这样算刚开始入门了吧,接下来会更深刻的学习。
做者:前程明亮
出处:http://www.cnblogs.com/0zcl
识别图中二维码,领取python全套视频资料