网站内部链接:

Basic Introduction

restful

在介绍 djangorestframework 框架之前让我们先简单的介绍一下REST框架,更加详细的介绍见文档

组件介绍

Requests

Reference: requests

Code: request.py

Quote: If you’re doing REST-based web service stuff … you should ignore request.POST.

Function: 扩展标准 HttpRequest, 提供更加灵活的 requests, 支持 requests parsing 和 request authentication.

Responses

Reference: responses

Function: 支持内容协商, 根据 content_type 来返回指定类型, 一般情况下是application/json类型

Code: response.py

Based Views

Reference: Base Views

Code: views.py, decorators.py

Function: class-based views is the way they allow you to compose(组合) bits of reusable(可重用) behavior.

class-based Views: APIView 类, 继承 View 类, 其中两者的不同之处: 返回 Response 实例, 捕获任何 APIException 异常, 自动检测权限以及其他.

Function-based Views: 利用@api_view()来实现 view 方法

Generic Views

Reference: Generic Views

Function: REST framework takes advantage of this by providing a number of pre-built views that provide for commonly used patterns.使用通用的模式预先构建的视图,让你快速的构建 View API, 这些 View 一般都是与 DB 模型密切相关.

Code: generics.py, mlxins.py

Mixin Class: 利用多重继承, 但是命名为 Mixin 类, 表示这是一个接口使用方式.

python mixin to replace implement,

Why mixin is useful

ViewSets

Reference: ViewSets

Function: Controllers/Resources, 将功能相近的一组 view 放在一个单独的类中. 默认情况, 不提供 get/post, 使用 list/create 替代. 通过注册机制来完成映射.

Code: viewsets.py

Routers

Reference: routers

Function: Resources routing, REST framework adds support for automatic URL routing to Django, and provides you with a simple, quick and consistent way of wiring your view logic to a set of URLs.

Code: routers.py

Nest Routers: nest routers

Serializers

Reference:
serializers,
serializer fields
serializer relations
Code: serializers.py, relations.py, fields.py

Function: 快速的转换 querysets, model 实例为 python 对象, 最后渲染为 JSON, XML 或者其他格式的数据并返回.

Validators

Reference: validators

Code: validators.py

Function: 自定义验证组件接口

Authentication

Reference: authentication

Code: authentication.py

Function: 认证管理

Permissions

Reference: permissions

Code: permissions.py

Function: 权限管理

Pagination

Reference: pagination

Code: pagination.py

Function: 分页控制

Exception

Reference: exceptions

Code: exceptions.py

Function: 异常处理

Status Code

Reference: status code

Code: status.py

Function: 对返回码的设置

Parsers

Reference: parsers

Code: parsers.py

Function: 接受各种类型的 Request, 解析各种不同的 midea types. 另外还可以自定义类型.

Renders

Reference: renders

Code: renders.py

Function: 对于不同的 Request Media Types, 返回各种不同的渲染类型, 从而使用各种不同的 Http Content Types.

Throtting

Reference: throtting

Code: throtting.py

Function: 429(或者 twitter-420), 限制请求访问频率

Filtering

Reference: filtering

Code: filters.py

Function: 过滤默认的返回结果.

Others

Versioning: 版本信息

Content Negotiation: 内容协商, 根据 Accept 头部来返回对应的类型

MetaData: 自定义 Http Response 的组成部分

Django Rest Swagger

Reference: Django REST Swagger