首页 > 其他 > 详细

DRF功能展示

时间:2020-10-28 09:21:43      阅读:2      评论:0      收藏:0      [点我收藏+]

1.1安装DjangoRestFramework

pip install djangorestframework==3.11.1 

pip install django-filter==2.3.0 

pip install markdown 

 

1.2.syl/settings.py配置

INSTALLED_APPS = [
    django_filters,
    rest_framework,

]

 

1.3 syl/settings.py 配置DRF:全局配置

REST_FRAMEWORK = {

    # 文档报错: AttributeError: ‘AutoSchema’ object has no attribute ‘get_link’
    # 用下面的设置可以解决

    DEFAULT_SCHEMA_CLASS: rest_framework.schemas.AutoSchema,
    # 默认设置是:
    # ‘DEFAULT_SCHEMA_CLASS‘: ‘rest_framework.schemas.openapi.AutoSchema‘,

    # 异常处理器
    # ‘EXCEPTION_HANDLER‘: ‘user.utils.exception_handler‘,
    # Base API policies
    DEFAULT_RENDERER_CLASSES: [
    rest_framework.renderers.JSONRenderer,
    rest_framework.renderers.BrowsableAPIRenderer,
],
DEFAULT_PARSER_CLASSES: [
    rest_framework.parsers.JSONParser,
    rest_framework.parsers.FormParser,
    rest_framework.parsers.MultiPartParser
],

# 1.认证器(全局)
DEFAULT_AUTHENTICATION_CLASSES: [
    # ‘rest_framework.authentication.SessionAuthentication‘,# 使用session时的认证器
    # ‘rest_framework.authentication.BasicAuthentication‘# 提交表单时的认证器
    # ‘rest_framework_jwt.authentication.JSONWebTokenAuthentication‘,# 在DRF中配置JWT认证
],

# 2.权限配置(全局): 顺序靠上的严格
DEFAULT_PERMISSION_CLASSES: [
        # ‘rest_framework.permissions.IsAdminUser‘, # 管理员可以访问
        # ‘rest_framework.permissions.IsAuthenticated‘, # 认证用户可以访问
        # ‘rest_framework.permissions.IsAuthenticatedOrReadOnly‘, # 认证用户可以访问, 否则只能读取
        #  ‘rest_framework.permissions.AllowAny‘, # 所有用户都可以访问
],

# 3.限流(防爬虫)
DEFAULT_THROTTLE_CLASSES: [
        rest_framework.throttling.AnonRateThrottle,
        rest_framework.throttling.UserRateThrottle,
],
# 3.1限流策略
DEFAULT_THROTTLE_RATES: {
    user: 1000/hour,  # 认证用户每小时100次
    anon: 3/day,  # 未认证用户每天能访问3次
},

DEFAULT_CONTENT_NEGOTIATION_CLASS: rest_framework.negotiation.DefaultContentNegotiation,
DEFAULT_METADATA_CLASS: rest_framework.metadata.SimpleMetadata,
DEFAULT_VERSIONING_CLASS: None,

# 4.分页(全局):全局分页器, 例如 省市区的数据自定义分页器, 不需要分页
# ‘DEFAULT_PAGINATION_CLASS‘: ‘rest_framework.pagination.PageNumberPagination‘,
# 每页返回数量
# ‘PAGE_SIZE‘: 5,  # 默认 None


# 5.过滤器后端
DEFAULT_FILTER_BACKENDS: [
    django_filters.rest_framework.DjangoFilterBackend,
    # ‘django_filters.rest_framework.backends.DjangoFilterBackend‘, 包路径有变化
],

# 5.1过滤排序(全局):Filtering 过滤排序
SEARCH_PARAM: search,
ORDERING_PARAM: ordering,
NUM_PROXIES: None,

# 6.版本控制:Versioning 接口版本控制
DEFAULT_VERSION: None,
ALLOWED_VERSIONS: None,
VERSION_PARAM: version,

# Authentication 认证
# 未认证用户使用的用户类型
UNAUTHENTICATED_USER: django.contrib.auth.models.AnonymousUser,
# 未认证用户使用的Token值
UNAUTHENTICATED_TOKEN: None,

# View configuration
VIEW_NAME_FUNCTION: rest_framework.views.get_view_name,
VIEW_DESCRIPTION_FUNCTION: rest_framework.views.get_view_description,
NON_FIELD_ERRORS_KEY: non_field_errors,

# Testing
TEST_REQUEST_RENDERER_CLASSES: [
    rest_framework.renderers.MultiPartRenderer,
    rest_framework.renderers.JSONRenderer
],
TEST_REQUEST_DEFAULT_FORMAT: multipart,

# Hyperlink settings
URL_FORMAT_OVERRIDE: format,
FORMAT_SUFFIX_KWARG: format,
URL_FIELD_NAME: url,

# Encoding
UNICODE_JSON: True,
COMPACT_JSON: True,
STRICT_JSON: True,
COERCE_DECIMAL_TO_STRING: True,
UPLOADED_FILES_USE_URL: True,

# Browseable API
HTML_SELECT_CUTOFF: 1000,
HTML_SELECT_CUTOFF_TEXT: "More than {count} items...",

# Schemas
SCHEMA_COERCE_PATH_PK: True,
SCHEMA_COERCE_METHOD_NAMES: {
        retrieve: read,
        destroy: delete
},
}

 

 

2.DRF 认证,权限,限流,分页过滤,序列化重定向

 

DRF功能展示

原文:https://www.cnblogs.com/xiaoxiamiaichiyu/p/13888861.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号