1. 程式人生 > >頻率元件,解析器

頻率元件,解析器

頻率元件:
-頻率是什麼?節流,訪問控制

-內建的訪問頻率控制類SimpleRateThrottle
-寫一個類,繼承SimpleRateThrottle
-class MyThrottle(SimpleRateThrottle):
scope='aaa'
def get_cache_key(self, request, view):
return self.get_ident(request)
-在setting中:
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_RATES':{
'aaa':'10/m'
}
}

-使用
區域性使用:
-在檢視類中寫
throttle_classes = [MyThrottle,]
全域性使用:
-在setting中寫
'DEFAULT_THROTTLE_CLASSES':['app01.MyAuth.MyThrottle',],

-區域性禁用:
-在檢視類中寫
throttle_classes = []



錯誤資訊改成中文顯示:
def throttled(self, request, wait):
class MyThrottled(exceptions.Throttled):
default_detail = '傻逼'
extra_detail_singular = '還剩 {wait} 秒.'
extra_detail_plural = '還剩 {wait} 秒'

raise MyThrottled(wait)


解析器:
作用:傳過來的資料,解析成字典
使用:
區域性使用:
from rest_framework.parsers import JSONParser,FormParser
在檢視類中:
parser_classes = [FormParser,]
全域性使用
REST_FRAMEWORK = {
'DEFAULT_PARSER_CLASSES':[
'rest_framework.parsers.JSONParser'
]

}

區域性使用指定的解析器:
在檢視類中:
parser_classes = [FormParser,]