認(rèn)證Authentication
可以在配置文件中配置全局默認(rèn)的認(rèn)證方案
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication', # 基本認(rèn)證
'rest_framework.authentication.SessionAuthentication', # session認(rèn)證
)
}
也可以在每個(gè)視圖中通過設(shè)置authentication_classess屬性來設(shè)置
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.views import APIView
class ExampleView(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
...
認(rèn)證失敗會(huì)有兩種可能的返回值:
401 Unauthorized 未認(rèn)證
403 Permission Denied 權(quán)限被禁止
權(quán)限Permissions
權(quán)限控制可以限制用戶對(duì)于視圖的訪問和對(duì)于具體數(shù)據(jù)對(duì)象的訪問侨艾。在執(zhí)行視圖的dispatch()方法前,會(huì)先進(jìn)行視圖訪問權(quán)限的判斷在通過get_object()獲取具體對(duì)象時(shí)拓挥,會(huì)進(jìn)行對(duì)象訪問權(quán)限的判斷
使用
可以在配置文件中設(shè)置默認(rèn)的權(quán)限管理類唠梨,如
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
)
}
如果未指明,則采用如下默認(rèn)配置
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
也可以在具體的視圖中通過permission_classes屬性來設(shè)置侥啤,如
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)
...
提供的權(quán)限
AllowAny 允許所有用戶
IsAuthenticated 僅通過認(rèn)證的用戶
IsAdminUser 僅管理員用戶
IsAuthenticatedOrReadOnly 認(rèn)證的用戶可以完全操作当叭,否則只能get讀取
自定義權(quán)限
如需自定義權(quán)限,需繼承rest_framework.permissions.BasePermission父類盖灸,并實(shí)現(xiàn)以下兩個(gè)任何一個(gè)方法或全部
.has_permission(self, request, view)是否可以訪問視圖蚁鳖, view表示當(dāng)前視圖對(duì)象
.has_object_permission(self, request, view, obj),是否可以訪問數(shù)據(jù)對(duì)象, view表示當(dāng)前視圖赁炎, obj為數(shù)據(jù)對(duì)象
限流Throttling
可以對(duì)接口訪問的頻次進(jìn)行限制醉箕,以減輕服務(wù)器壓力。
使用
可以在配置文件中,使用DEFAULT_THROTTLE_CLASSES 和 DEFAULT_THROTTLE_RATES進(jìn)行全局配置琅攘,
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, hour 或day來指明周期垮庐。也可以在具體視圖中通過throttle_classess屬性來配置,如
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
class ExampleView(APIView):
throttle_classes = (UserRateThrottle,)
...
可選限流類
1) AnonRateThrottle
限制所有匿名未認(rèn)證用戶坞琴,使用IP區(qū)分用戶哨查。
使用DEFAULT_THROTTLE_RATES['anon'] 來設(shè)置頻次
2)UserRateThrottle
限制認(rèn)證用戶,使用User id 來區(qū)分剧辐。使用DEFAULT_THROTTLE_RATES['user'] 來設(shè)置頻次
3)ScopedRateThrottle
限制用戶對(duì)于每個(gè)視圖的訪問頻次寒亥,使用ip或user id。
過濾Filtering
對(duì)于列表數(shù)據(jù)可能需要根據(jù)字段進(jìn)行過濾荧关,我們可以通過添加django-fitlter擴(kuò)展來增強(qiáng)支持溉奕。
pip insall django-filter
在配置文件中增加過濾后端的設(shè)置:
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=西游記
分頁(yè)P(yáng)agination
REST framework提供了分頁(yè)的支持忍啤。我們可以在配置文件中設(shè)置全局的分頁(yè)方式加勤,如:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 100 # 每頁(yè)數(shù)目
}
也可通過自定義Pagination類,來為視圖添加不同分頁(yè)行為同波。在視圖中通過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
可選分頁(yè)器
1) PageNumberPagination
前端訪問網(wǎng)址形式:
GET http://api.example.org/books/?page=4
可以在子類中定義的屬性:
page_size 每頁(yè)數(shù)目
page_query_param 前端發(fā)送的頁(yè)數(shù)關(guān)鍵字名,默認(rèn)為"page"
page_size_query_param 前端發(fā)送的每頁(yè)數(shù)目關(guān)鍵字名未檩,默認(rèn)為None
max_page_size 前端最多能設(shè)置的每頁(yè)數(shù)量
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
2)LimitOffsetPagination
前端訪問網(wǎng)址形式:
GET http://api.example.org/books/?limit=100&offset=400
可以在子類中定義的屬性:
default_limit 默認(rèn)限制戴尸,默認(rèn)值與PAGE_SIZE設(shè)置一直
limit_query_param limit參數(shù)名,默認(rèn)'limit'
offset_query_param offset參數(shù)名冤狡,默認(rèn)'offset'
max_limit 最大limit限制孙蒙,默認(rèn)None