django-rest-framework

Django REST框架是一个用于构建Web API的功能强大且灵活的工具包

REST框架的强大功能:

  • Web可以浏览的API对于开发人员来说是一个极大的可用性
  • 身份认证策略包括OAuth 1a 和 OAuth2a的软件包
  • 支持ORM和非ORM数据源的序列化
  • 可基于常规的功能视图完全自定义
  • 有广泛的文档和良好的社区支持

 使用REST框架的要求

  • Python(2.7, 3.2, 3.3, 3.4, 3.5, 3.6)
  • Django(1.10, 1.11, 2.0)

 以下软件包是可选的

  • coreapi 模式生成支持
  • Markdown 可浏览API的Markdown支持
  • django-filter 过滤支持
  • django-crispy-forms 改进了用于过滤的HTML显示
  • django-guardian 对象级权限支持

 安装

使用pip安装,以及任何你需要安装的软件包  注:安装djangorestframework之前一定要先安装django

pip install djangorestframework
pip install markdown       
pip install django-filter

或者从github克隆项目

git clone git@github.com:encode/django-rest-framework.git

将'rest_framework'添加的django的settings.py的INSTALLED_APPS设置中

INSTALLED_APPS = (
    ...
    'rest_framework',
)

添加到urls.py文件中,即可使用可浏览的API

from django.conf.urls import url, include
urlpatterns = [
    ...
    url(r'^api-auth/', include('rest_framework.urls'))
]

REST框架API的任何全局设置都保存在一个名为REST_FRAMEWORK的配置字典中,首先要将内容配置到settings.py模块中

举例说明:

我们来创建一个API来对我们对数据进行分页

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
     'PAGE_SIZE': 10, # 每页显示多少条数据 
 }

首先对我们对数据进行序列化,在当前项目中创建serializers.py文件

from rest_framework import serializers
from goods.models import Goods, GoodsCategory  # django的model

class GoodsCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = GoodsCategory
        fields = "__all__"   # fields需要在页面显示的数据内容


class GoodsSerializer(serializers.ModelSerializer):
    category = GoodsCategorySerializer()

    class Meta:
        model = Goods
        # fields = ("category", "name", "market_price", "add_time", "goods_front_image")
        fields = "__all__"  

创建视图,views.py

from .models import Goods
from .serializers import GoodsSerializer
from rest_framework import mixins
from rest_framework import viewsets


class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer

urls.py

from django.conf.urls import url, include
from rest_framework.documentation import include_docs_urls
from goods.views import GoodsListViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'goods', GoodsListViewSet)


urlpatterns =[
    url(r'^api-auth/', include('rest_framework.urls')),
    url(r'^', include(router.urls)),


]

现在就可以通过http://127.0.0.1:8000/goods/的浏览器中打开该API,并查看新的“用户”API。如果您使用右上角的登录控件,您还可以从系统添加,创建和删除用户。

拓展功能,如果你需要自定制分页内容,比如:每页最多显示多少条数据,你想要在这一页像后台指定访问多少条数据,下一页页码标示等

views.py更改如下

from .models import Goods
from .serializers import GoodsSerializer
from rest_framework import mixins, generics
from rest_framework.pagination import PageNumberPagination
from rest_framework import viewsets


class GoodsSetPagination(PageNumberPagination):
    page_size = 10  # 每一天显示10条数据
    page_size_query_param = 'page_size'  # 像后台请求前端要显示页码的大小(条数)
    page_query_param = 'p'  # 浏览器导航栏下一页的提示
    max_page_size = 100  # 每页最多显示多少条


class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = GoodsSetPagination

将settings.py模块中以下内容注释掉,因为views.py中的

class GoodsSetPagination(PageNumberPagination)以及继承了
PageNumberPagination
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
     'PAGE_SIZE': 10, # 每页显示多少条数据 
 }

组成REST框架的构建块,它会让你全面了解所有内容组合到一起的强大功能,强烈推荐学习

 API指南

  • Requests
  • Responses
  • Views
  • Generic views
  • Viewsets
  • Routers
  • Parsers
  • Renderers
  • Serializers
  • Serializer fields
  • Serializer relations
  • Validators
  • Authentication
  • Permissions
  • Throttling
  • Filtering
  • Pagination
  • Versioning
  • Content negotiation
  • Metadata
  • Schemas
  • Format suffixes
  • Returning URLs
  • Exceptions
  • Status codes
  • Testing
  • Settings
原文地址:https://www.cnblogs.com/YingLai/p/9133399.html