django rest_framework
2019-10-10 本文已影响0人
Oscar_hailiang
1. 渲染器
渲染器格式如下:
from rest_framework.renderers import JSONRenderer, ....等等
1. JSONRenderer # 序列化为json的呈现程序
2. AdminRender # 表格格式
3. TemplateHTMLRenderer # 用于模板的HTML呈现程序
4. StaticHTMLRenderer # 一个html呈现器类,它只返回预先呈现的html
5. HTMLFormRenderer # 渲染器将数据序列化为HTML表单。
6. BrowsableAPIRenderer # 浏览器的html呈现程序
7. AdminRenderer # 基于rest_framework/admin.html渲染数据
8. DocumentationRenderer # ...
9. SchemaJSRenderer #
10. MultiPartRenderer
11. CoreJSONRenderer # 基于coreapi的json.
2. 分页器
# 引用分页器
from rest_framework.pagination import PageNumberPagination, \
LimitOffsetPagination, \
CursorPagination
分页器有如下几种 :
(1) PageNumberPagination
一种简单的基于页码的样式,支持将页码作为查询参数。例如:
- http://api.example.org/accounts/?page=4
-
http://api.example.org/accounts/?page=4&page_size=100
配置参数如下:
# The default page size.
# Defaults to `None`, meaning pagination is disabled.
page_size = api_settings.PAGE_SIZE
django_paginator_class = DjangoPaginator
# Client can control the page using this query parameter.
page_query_param = 'page'
page_query_description = _('A page number within the paginated result set.')
# Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.')
# Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None # 每页最多显示多少个
last_page_strings = ('last',)
(2) LimitOffsetPagination
基于限制/偏移的样式。例如:
http://api.example.org/accounts/?limit=100
http://api.example.org/accounts/?offset=400&limit=100
配置参数如下:
default_limit = api_settings.PAGE_SIZE
limit_query_param = 'limit'
limit_query_description = _('Number of results to return per page.')
offset_query_param = 'offset'
offset_query_description = _('The initial index from which to return the results.')
max_limit = None
(3) CursorPagination
游标分页实现必然很复杂。有关我们使用的位置/偏移样式的概述,请参阅以下文章:https://cra.mr/2011/03/08/building-cursors-for-the-disks-api
主要配置参数如下:
cursor_query_param = 'cursor'
cursor_query_description = _('The pagination cursor value.')
page_size = api_settings.PAGE_SIZE
invalid_cursor_message = _('Invalid cursor')
ordering = '-created'
template = 'rest_framework/pagination/previous_and_next.html'
# Client can control the page size using this query parameter.
# Default is 'None'. Set to eg 'page_size' to enable usage.
page_size_query_param = None
page_size_query_description = _('Number of results to return per page.')
# Set to an integer to limit the maximum page size the client may request.
# Only relevant if 'page_size_query_param' has also been set.
max_page_size = None
# The offset in the cursor is used in situations where we have a
# nearly-unique index. (Eg millisecond precision creation timestamps)
# We guard against malicious users attempting to cause expensive database
# queries, by having a hard cap on the maximum possible size of the offset.
offset_cutoff = 1000
3. 视图
1. django的CBV
class User(View):
pass
2. rest_framework - APIView
class User(APIView)
3. rest_framework - APIView
from api.utils.seriallizsers.user import UserSerialiser
from rest_framework.generics import GenericAPIView
class User(GenericAPIView):
queryset = model.object.all()
serializer_class = UserSerialiser
def get(self, request, *args, **kwargs):
# 获取数据
users = self.get_queryset() # models.User.objects.all()
# 序列化
ser = self.get_serializer(instance=users, many=True)
return Response(ser.data)
3. rest_framework - GenericViewSet
from api.utils.seriallizsers.user import UserSerialiser
from rest_framework.generics import GenericViewSet
# url定义时,需要在as_view中添加参数“{'get': 'list'}”,例如:path('user/', user.as_view({'get': 'list'}))
class User(GenericAPIView):
queryset = model.object.all()
serializer_class = UserSerialiser
def list(self, request, *args, **kwargs):
# 获取数据
users = self.get_queryset() # models.User.objects.all()
# 序列化
ser = self.get_serializer(instance=users, many=True)
return Response(ser.data)