drf基础:其他功能 | django |《python学习之路》| python 技术论坛-江南app体育官方入口

未匹配的标注

认证

使用

可以在配置文件中配置全局默认的认证方案

rest_framework = {
    'default_authentication_classes': (
        'rest_framework.authentication.basicauthentication',   # 基本认证
        'rest_framework.authentication.sessionauthentication',  # session认证
    )
}

也可以在每个视图中通过设置authentication_classess属性来设置

from rest_framework.authentication import sessionauthentication, basicauthentication
from rest_framework.views import apiview
class exampleview(apiview):
    authentication_classes = (sessionauthentication, basicauthentication)
    ...

返回值

认证失败会有两种可能的返回值:

  • 401 unauthorized 未认证
  • 403 permission denied 权限被禁止

权限

简介

权限控制可以限制用户对于视图的访问和对于具体数据对象的访问。

  • 在执行视图的dispatch()方法前,会先进行视图访问权限的判断
  • 在通过get_object()获取具体对象时,会进行对象访问权限的判断

使用

可以在配置文件中设置默认的权限管理类,如

rest_framework = {
    'default_permission_classes': (
        'rest_framework.permissions.isauthenticated',
    )
}

如果未指明,则采用如下默认配置

'default_permission_classes': (
   'rest_framework.permissions.allowany',
)

也可以在具体的视图中通过permission_classes属性来设置,如

from rest_framework.permissions import isauthenticated
from rest_framework.views import apiview
class exampleview(apiview):
    permission_classes = (isauthenticated,)
    ...

提供的权限

  • allowany 允许所有用户
  • isauthenticated 仅通过认证的用户
  • isadminuser 仅管理员用户
  • isauthenticatedorreadonly 认证的用户可以完全操作,否则只能get读取

举例

from rest_framework.authentication import sessionauthentication
from rest_framework.permissions import isauthenticated
from rest_framework.generics import retrieveapiview
class bookdetailview(retrieveapiview):
    queryset = bookinfo.objects.all()
    serializer_class = bookinfoserializer
    authentication_classes = [sessionauthentication]
    permission_classes = [isauthenticated]

自定义权限

如需自定义权限,需继承rest_framework.permissions.basepermission父类,并实现以下两个任何一个方法或全部

  • .has_permission(self, request, view)

    是否可以访问视图, view表示当前视图对象

  • .has_object_permission(self, request, view, obj)

    是否可以访问数据对象, view表示当前视图, obj为数据对象

例如:

class mypermission(basepermission):
    def has_object_permission(self, request, view, obj):
        """控制对obj对象的访问权限,此案例决绝所有对对象的访问"""
        return false
class bookinfoviewset(modelviewset):
    queryset = bookinfo.objects.all()
    serializer_class = bookinfoserializer
    permission_classes = [isauthenticated, mypermission]

限流

简介

可以对接口访问的频次进行限制,以减轻服务器压力。

使用

可以在配置文件中,使用default_throttle_classesdefault_throttle_rates进行全局配置,

rest_framework = {
    'default_throttle_classes': (
        'rest_framework.throttling.anonratethrottle',
        'rest_framework.throttling.userratethrottle'
    ),
    'default_throttle_rates': {
        'anon': '100/day',
        'user': '1000/day'
    }
}

default_throttle_rates 可以使用 second, minute, hourday来指明周期。

也可以在具体视图中通过throttle_classess属性来配置,如

from rest_framework.throttling import userratethrottle
from rest_framework.views import apiview
class exampleview(apiview):
    throttle_classes = (userratethrottle,)
    ...

可选限流类

  • anonratethrottle:限制所有匿名未认证用户,使用ip区分用户。使用default_throttle_rates['anon'] 来设置频次

  • userratethrottle:限制认证用户,使用user id 来区分。使用default_throttle_rates['user'] 来设置频次

  • scopedratethrottle:限制用户对于每个视图的访问频次,使用ip或user id。

过滤

简介

对于列表数据可能需要根据字段进行过滤,我们可以通过添加django-fitlter扩展来增强支持。

pip insall django-filter

使用

在配置文件中增加过滤后端的设置:

installed_apps = [
    ...
    'django_filters',  # 需要注册应用,
]
rest_framework = {
    'default_filter_backends': ('django_filters.rest_framework.djangofilterbackend',)
}

在视图中添加filter_fields属性,指定可以过滤的字段

class booklistview(listapiview):
    queryset = bookinfo.objects.all()
    serializer_class = bookinfoserializer
    filter_fields = ('btitle', 'bread')
# 127.0.0.1:8000/books/?btitle=西游记

排序

简介

对于列表数据,rest framework提供了orderingfilter过滤器来帮助我们快速指明数据按照指定字段进行排序。

使用

在类视图中设置filter_backends,使用rest_framework.filters.orderingfilter过滤器,rest framework会在请求的查询字符串参数中检查是否包含了ordering参数,如果包含了ordering参数,则按照ordering参数指明的排序字段对数据集进行排序。

前端可以传递的ordering参数的可选字段值需要在ordering_fields中指明。

分页

简介

rest framework提供了分页的支持。

使用

我们可以在配置文件中设置全局的分页方式,如:

rest_framework = {
    'default_pagination_class':  'rest_framework.pagination.pagenumberpagination',
    'page_size': 100  # 每页数目
}

也可通过自定义pagination类,来为视图添加不同分页行为。在视图中通过pagination_clas属性来指明。

class largeresultssetpagination(pagenumberpagination):
    page_size = 1000
    page_size_query_param = 'page_size'
    max_page_size = 10000
class bookdetailview(retrieveapiview):
    queryset = bookinfo.objects.all()
    serializer_class = bookinfoserializer
    pagination_class = largeresultssetpagination

注意:如果在视图内关闭分页功能,只需在视图内设置

pagination_class = none

可选分页器

pagenumberpagination

前端访问网址形式:

get  http://api.example.org/books/?page=4

可以在子类中定义的属性:

  • page_size 每页数目
  • page_query_param 前端发送的页数关键字名,默认为”page”
  • page_size_query_param 前端发送的每页数目关键字名,默认为none
  • max_page_size 前端最多能设置的每页数量
from rest_framework.pagination import pagenumberpagination
class standardpagenumberpagination(pagenumberpagination):
    page_size_query_param = 'page_size'
    max_page_size = 10
class booklistview(listapiview):
    queryset = bookinfo.objects.all().order_by('id')
    serializer_class = bookinfoserializer
    pagination_class = standardpagenumberpagination
# 127.0.0.1/books/?page=1&page_size=2

limitoffsetpagination

前端访问网址形式:

get http://api.example.org/books/?limit=100&offset=400

可以在子类中定义的属性:

  • default_limit 默认限制,默认值与page_size设置一直
  • limit_query_param limit参数名,默认’limit’
  • offset_query_param offset参数名,默认’offset’
  • max_limit 最大limit限制,默认none
from rest_framework.pagination import limitoffsetpagination
class booklistview(listapiview):
    queryset = bookinfo.objects.all().order_by('id')
    serializer_class = bookinfoserializer
    pagination_class = limitoffsetpagination
# 127.0.0.1:8000/books/?offset=3&limit=2

版本

简介

rest framework提供了版本号的支持。在需要获取请求的版本号时,可以通过request.version来获取。默认版本功能未开启,request.version 返回none。

使用

开启版本支持功能,需要在配置文件中设置default_versioning_class

rest_framework = {
    'default_versioning_class': 'rest_framework.versioning.namespaceversioning'
}

其他可选配置:

  • default_version 默认版本号,默认值为none
  • allowed_versions 允许请求的版本号,默认值为none
  • version_param 识别版本号参数的名称,默认值为’version’

支持的版本处理方式

acceptheaderversioning

请求头中传递的accept携带version

get /bookings/ http/1.1
host: example.com
accept: application/json; version=1.0

urlpathversioning

url路径中携带

urlpatterns = [
    url(
        r'^(?p(v1|v2))/bookings/$',
        bookings_list,
        name='bookings-list'
    ),
    url(
        r'^(?p(v1|v2))/bookings/(?p[0-9] )/$',
        bookings_detail,
        name='bookings-detail'
    )
]

namespaceversioning

命名空间中定义

# bookings/urls.py
urlpatterns = [
    url(r'^$', bookings_list, name='bookings-list'),
    url(r'^(?p[0-9] )/$', bookings_detail, name='bookings-detail')
]
# urls.py
urlpatterns = [
    url(r'^v1/bookings/', include('bookings.urls', namespace='v1')),
    url(r'^v2/bookings/', include('bookings.urls', namespace='v2'))
]

hostnameversioning

主机域名携带

get /bookings/ http/1.1
host: v1.example.com
accept: application/json

queryparameterversioning

查询字符串携带

get /something/?version=0.1 http/1.1
host: example.com
accept: application/json

异常处理

简介

rest framework提供了异常处理,我们可以自定义异常处理函数。

使用

自定义异常处理函数

from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
    # 先调用rest framework默认的异常处理方法获得标准错误响应对象
    response = exception_handler(exc, context)
    # 在此处补充自定义的异常处理
    if response is not none:
        response.data['status_code'] = response.status_code
    return response

在配置文件中声明自定义的异常处理

rest_framework = {
    'exception_handler': 'my_project.my_app.utils.custom_exception_handler'
}

如果未声明,会采用默认的方式,如下

rest_framework = {
    'exception_handler': 'rest_framework.views.exception_handler'
}

例如:

补充上处理关于数据库的异常

from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework import status
from django.db import databaseerror
def exception_handler(exc, context):
    response = drf_exception_handler(exc, context)
    if response is none:
        view = context['view']
        if isinstance(exc, databaseerror):
            print('[%s]: %s' % (view, exc))
            response = response({'detail': '服务器内部错误'}, status=status.http_507_insufficient_storage)
    return response

rest framework定义的异常

  • apiexception 所有异常的父类
  • parseerror 解析错误
  • authenticationfailed 认证失败
  • notauthenticated 尚未认证
  • permissiondenied 权限决绝
  • notfound 未找到
  • methodnotallowed 请求方式不支持
  • notacceptable 要获取的数据格式不支持
  • throttled 超过限流次数
  • validationerror 校验失败

自动生成接口文档

简介

rest framework可以自动帮助我们生成接口文档。接口文档以网页的方式呈现。自动接口文档能生成的是继承自apiview及其子类的视图。

使用

安装依赖

rest framewrok生成接口文档需要coreapi库的支持。

pip install coreapi

设置接口文档访问路径

在总路由中添加接口文档路径。

文档路由对应的视图配置为rest_framework.documentation.include_docs_urls

参数title为接口文档网站的标题。

from rest_framework.documentation import include_docs_urls
urlpatterns = [
    ...
    url(r'^docs/', include_docs_urls(title='my api title'))
]

文档描述说明的定义位置

  • 单一方法的视图,可直接使用类视图的文档字符串,如

    class booklistview(generics.listapiview):
        """
        返回所有图书信息.
        """
  • 包含多个方法的视图,在类视图的文档字符串中,分开方法定义,如

    class booklistcreateview(generics.listcreateapiview):
        """
        get:
        返回所有图书信息.
        post:
        新建图书.
        """
  • 对于视图集viewset,仍在类视图的文档字符串中封开定义,但是应使用action名称区分,如

    class bookinfoviewset(mixins.listmodelmixin, mixins.retrievemodelmixin, genericviewset):
        """
        list:
        返回图书列表数据
        retrieve:
        返回图书详情数据
        latest:
        返回最新的图书数据
        read:
        修改图书的阅读量
        """

访问接口文档网页

浏览器访问 127.0.0.1:8000/docs/,即可看到自动生成的接口文档。

两点说明

  • 视图集viewset中的retrieve名称,在接口文档网站中叫做read

  • 参数的description需要在模型类或序列化器类的字段中以help_text选项定义

本文章首发在 江南app体育官方入口 网站上。

上一篇 下一篇
讨论数量: 0



暂无话题~
网站地图