Django-REST-framework Views和ViewSets学习
基于类的视图
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from django.contrib.auth.models import User
class ListUsers(APIView):
authentication_classes = (authentication.TokenAuthentication,)
permission_classes = (permissions.IsAdminUser,)
def get(self, request, format=None):
usernames = [user.username for user in User.objects.all()]
return Response(usernames)
def post(self, request, *args, **kwargs):
...API策略属性
dispatch()调度过程
基于函数的视图
通用视图
GenericAPIView
Mixins
具体视图类
ViewSets
简单视图集
标记额外路由行为
ViewSet基类
Last updated