#暴露只读的接口、支持get、head、options方法,禁用前端更改数据
from rest_framework import permissions #引入权限模块
#在自己的app中定义自己的权限模块,如:Item/permissions.py
SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS')
class ReadOnly(permissions.BasePermission):
"""
The model can't be update!
"""
def has_permission(self, request, view):
return (
request.method in SAFE_METHODS
)
继承该权限函数后,重写has_permission方法,对于每一次请求,根据该函数的返回值,决定是否有权限。