您好,登錄后才能下訂單哦!
一、restful介紹
restful是一種接口規范,前后端根據這種規范開發相應的接口,提高團隊開發效率,尤其用于前后端分離,根據規范前后端開發模塊互不影響
二、路由
from rest_framework.routers import SimpleRouter,DefaultRouter
在routers模塊下 封裝了很多關于路由的方法 , 最基礎的BaseRouter類,提供自定制的接口
下面這個方法給提供了自動生成兩條帶參數的url
router = DefaultRouter() # 注冊路由 繼承BaseRouter類
router.register('user',UserProfileViewset,base_name='useruinfo')
router.register('menu',MenuProfileViewset,base_name='menuinfo')
生成到view的路由映射
三、view視圖
幫助開發者提供了一些類,并在類中提供了多種方法供我們使用,下圖是提供的主要的類以及繼承關系
類介紹
1.APIView
class Nav(APIView):
"""
初始化菜單
"""
permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly)
authentication_classes = (JSONWebTokenAuthentication,)
def get(self,request):
content = getMenu(request)
if not content:
return JsonResponse(data=content, code=200, msg="菜單初始化失敗", flag=True)
else:
return JsonResponse(data=content, code=200, msg="菜單初始化成功", flag=True)
提供get,post,put,patch,delete五種方法
2.GenericAPIView
class IndexView(GenericAPIView):
queryset = models.UserInfo.objects.all()
serializer_class = UserInfoSerializer
lookup_field = 'pk'
def get(self,request,*args,**kwargs):
pk = kwargs.get('pk')
if pk:
users = self.filter_queryset(queryset=models.UserInfo.objects.get(pk=pk))
ser = self.get_serializer(instance=users)
else:
users = self.get_queryset()
ser = self.get_serializer(instance=users,many=True)
return Response(ser.data)
在GenericAPIView中要重寫一些字段和方法,不常用
3.GenericViewSet
class IndexView(GenericViewSet):
serializer_class = UserInfoSerializer
queryset = models.UserInfo.objects.all()
def create(self,request,*args,**kwargs):
pass
def list(self,request,*args,**kwargs): # 獲取列表數據
users = models.UserInfo.objects.all()
ser = UserInfoSerializer(instance=users,many=True)
return Response(ser.data)
def retrieve(self,request,*args,**kwargs): # 獲取單條數據
pk = kwargs.get('pk')
users = models.UserInfo.objects.get(pk=pk)
ser = UserInfoSerializer(instance=users,many=False)
return Response(ser.data)
def destroy(self,request,*args,**kwargs):
pass
def update(self,request,*args,**kwargs):
pass
def partial_update(self,request,*args,**kwargs):
pass
這個類繼承了ViewSetMixin, generics.GenericAPIView,其中在ViewSetMixin中會重寫as_view()方法,因此可以將URL中的請求方式與視圖函數綁定到一起,在urls.py中以鍵值對的方式存在
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^hehe/', views.hehe),
url(r'^index/$', views.IndexView.as_view({'get':'list','post':'create'})),
url(r'^index/(?P<pk>\d+)/$', views.IndexView.as_view({'get':'retrieve','put':'update','patch':'partial_update','delete':'destroy'})),]
4.ModelViewSet
ModelViewSet繼承了四個混入類和一個泛類,將會獲得增刪改查的所有方法
class ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
"""
A viewset that provides default `create()`, `retrieve()`, `update()`,
`partial_update()`, `destroy()` and `list()` actions.
"""
pass
四、認證
rest_framework給我們提供了認證的接口,由BaseAuthentication類提供接口,也有一些封裝好的認證類
接口函數 authticate 認證成功返回一元組(user,token)分別賦值給request.user 和 request.auth
class Auth(BaseAuthentication):
def authenticate(self, request):
token = request.query_params.get('token')
obj = models.Token.objects.filter(token=token).first()
if not obj:
raise AuthenticationFailed({'code': 1001, 'error': '認證失敗'})
return (obj.user.username, obj)
認證方式有多種,可以使用jwt認證
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
class UserProfileViewset(custom_viewset_base.CustomViewBase):
"""
permission_classes,authentication_classes放置順序不能變
"""
permission_classes = (IsOwnerOrReadOnly,)
authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication)
serializer_class = UserProfileSerializer
queryset = User.objects.all()
pagination_class = custom_pagination.LargeResultsSetPagination
filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
filter_class = UserProfileFilter
search_fields = ('username', ) # ^以什么開頭匹配,=等于匹配 "__all__"
ordering_fields = ('username',) # 排序
ordering = ('username',) # 排序字段
五、權限
由BasePermission類給提供接口 接口函數為 has_permission 以及 has_object_permission
class BasePermission(object):
"""
A base class from which all permission classes should inherit.
"""
def has_permission(self, request, view):
"""
Return `True` if permission is granted, `False` otherwise.
"""
# 這里寫我們的權限邏輯
return True
def has_object_permission(self, request, view, obj):
"""
Return `True` if permission is granted, `False` otherwise.
"""
return True
可以重寫該方法,來控制訪問權限
六、序列化
對queryset序列化以及對請求數據格式驗證。
通常繼承兩個類 Serializer 以及 ModelSerializer
Serializer 序列化的每個字段都要自己寫 ModelSerializer 會根據數據庫表渲染所有字段
class CourseDetailModelSerializers(serializers.ModelSerializer):
title = serializers.CharField(source='course.name')
img = serializers.ImageField(source='course.course_img')
level = serializers.CharField(source='course.get_level_display')
recommends = serializers.SerializerMethodField()
chapters = serializers.SerializerMethodField()
def get_recommends(self, obj):
queryset = obj.recommend_courses.all()
return [{'id': row.id, 'title': row.name} for row in queryset]
def get_chapters(self, obj):
queryset = obj.course.course_chapters.all()
return [{'id': row.id, 'name': row.name} for row in queryset]
class Meta:
model = CourseDetail
fields = ['course', 'title', 'img', 'level', 'why_study', 'chapters', 'recommends']
七、分頁
自定義分頁
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination
from collections import OrderedDict
from rest_framework.response import Response
class LargeResultsSetPagination(PageNumberPagination):
page_size = 10 # 每頁默認顯示條數
page_size_query_param = "page_size" # 每頁顯示條數傳參字符串
page_query_param = "page" # 顯示第幾頁數據傳參
max_page_size = 100 # 最大頁碼
def get_paginated_response(self, data):
code = 200
msg = '查詢成功'
if not data:
code = 200
msg = "數據為空"
return Response(OrderedDict([
('code', code),
('msg', msg),
('flag', True),
('count', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
在view中引入
pagination_class = LargeResultsSetPagination
八、過濾器
使用django_filters模塊
import django_filters
from user.models import *
class UserProfileFilter(django_filters.rest_framework.FilterSet):
"""
過濾用戶
"""
#id = django_filters.NumberFilter(field_name="id",lookup_expr="exact") # 精確匹配
username = django_filters.CharFilter(field_name="username",lookup_expr="contains")
#name = django_filters.CharFilter(field_name="name", lookup_expr="contains")
#mobile = django_filters.CharFilter(field_name="mobile", lookup_expr="contains")
class Meta:
model = UserProfile
fields = ["username"]
view視圖應用
filter_class = UserProfileFilter
九、渲染器
默認的兩個渲染器,一個是Json的,一個是用瀏覽器訪問rest_framework自帶的模板的
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。