drf認證 許可權 頻率 過濾 排序 異常處理

2021-10-10 09:01:11 字數 4238 閱讀 1835

使用方法:

1、新建乙個認證類檔案,繼承baseauthentication

raise authenticationfailed('驗證失敗')

else:

raise authenticationfailed('請求頭沒有token資訊')

2、認證類區域性配置

'''

區域性使用,在列表內發那個值認證類

區域性禁用,設定列表為空

'''class login(apiview):

#區域性使用

authentication_classes = [bookauth,]

#區域性禁用

# authentication_classes =

def post(self, request):

user = models.user.objects.filter(username=request.data.get('username'), password=request.data.get('password')).first()

if user:

token = uuid.uuid4()

models.usertoken.objects.update_or_create(user=user, defaults=)

return response("")

else:

return response("")

3、全域性配置,在settings檔案中配置:

rest_framework=

認證原始碼分析:

#1 apiview----》dispatch方法---》self.initial(request, *args, **kwargs)---->有認證,許可權,頻率

#2 唯讀認證原始碼: self.perform_authentication(request)

#3 self.perform_authentication(request)就一句話:request.user,需要去drf的request物件中找user屬性(方法)

#4 request類中的user方法,剛開始來,沒有_user,走 self._authenticate()

#5 核心,就是request類的 _authenticate(self):

def _authenticate(self):

# 遍歷拿到乙個個認證器,進行認證

# self.authenticators配置的一堆認證類產生的認證類物件組成的 list

#self.authenticators 你在檢視類中配置的乙個個的認證類:authentication_classes=[認證類1,認證類2],物件的列表

for authenticator in self.authenticators:

try:

# 認證器(物件)呼叫認證方法authenticate(認證類物件self, request請求物件)

# 返回值:登陸的使用者與認證的資訊組成的 tuple

# 該方法被try包裹,代表該方法會拋異常,拋異常就代表認證失敗

user_auth_tuple = authenticator.authenticate(self) #注意這self是request物件

except exceptions.apiexception:

self._not_authenticated()

raise

# 返回值的處理

if user_auth_tuple is not none:

self._authenticator = authenticator

# 如何有返回值,就將 登陸使用者 與 登陸認證 分別儲存到 request.user、request.auth

self.user, self.auth = user_auth_tuple

return

# 如果返回值user_auth_tuple為空,代表認證通過,但是沒有 登陸使用者 與 登陸認證資訊,代表遊客

self._not_authenticated()

許可權原始碼分析

# apiview---->dispatch---->initial--->self.check_permissions(request)(apiview的物件方法)

def check_permissions(self, request):

# 遍歷許可權物件列表得到乙個個許可權物件(許可權器),進行許可權認證

for permission in self.get_permissions():

# 許可權類一定有乙個has_permission許可權方法,用來做許可權認證的

# 引數:許可權物件self、請求物件request、檢視類物件

# 返回值:有許可權返回true,無許可權返回false

if not permission.has_permission(request, self):

self.permission_denied(

request, message=getattr(permission, 'message', none)

)

使用方法:

(1)自定義乙個許可權類,繼承basepermission類,**如下:

from rest_framework.permissions import basepermission

class user_per(basepermission):

def has_permission(self, request, view):

user=request.user

is_staff=user.is_staff

if is_staff==1: #1:管理員 2:普通使用者 3:匿名使用者

return true

else:

return false(2)全域性配置跟區域性配置:rest_framework = ,

}區域性配置:from rest_framework.throttling import userratethrottle

class testview1(apiview):

#頻率區域性配置

throttle_classes = [userratethrottle]

def get(self,request):

return response('我是匿名使用者')

使用方法:

1、安裝:pip install django-filter

'django.contrib.admin',

....'django_filters',]

3、全域性配置:

全域性配置:

rest_framework =

drf 過濾與排序

drf中如果想在url中直接寫過濾與排序,則需要安裝第三方模組。pip install django filter其次你需要將該模組註冊進行django配置檔案中。rest framework django filters 以下是一張書籍表和資料,我可能通過書名查,也可能通過 查詢。可能通過 排序,...

DRF的認證與許可權功能

在setting.py進行配置。rest framework from rest framework.authentication import sessionauthentication,basicauthentication from rest framework.views import ap...

DRF的過濾與排序

對於列表資料可能需要根據字段進行過濾,我們可以通過新增django filter擴充套件來增強支援。pip install django filter 在配置檔案中增加過濾後端的設定 django filters 需要註冊應用,在settings檔案中配置 rest framework 檢視中指定要...