zoukankan      html  css  js  c++  java
  • Django

    ModelViewSet源码

    class ModelViewSet(mixins.CreateModelMixin,

    mixins.RetrieveModelMixin,

    mixins.UpdateModelMixin,

    mixins.DestroyModelMixin,

    mixins.ListModelMixin,

    GenericViewSet):

    class UserViewSet(ModelViewSet): fields = ( swagger_doc(name="Authorization", description='用户token', location='header', required=False), ) # 认证 # authentication_classes = [Authentication, ] # queryset 和 serializer_class 必须指定 queryset = User.objects.all() serializer_class = CreateUserSerializer pagination_class = StandardResultsSetPagination def retrieve(self, request, pk=None): """ 用户详情 """ # 获取实例 user = self.get_object() # 序列化 serializer = self.get_serializer(user) return success_response(serializer.data) def create(self, request, *args, **kwargs): """ 创建用户 """ serializer = self.get_serializer(data=request.data) # save 前必须先调用 is_valid() serializer.is_valid(raise_exception=True) serializer.save() return success_response(serializer.data) def update(self, request: Request, *args, **kwargs): """ 更新用户信息 """ def destroy(self, request: Request, *args, **kwargs): """ 删除用户 """ return success_response() def list(self, request: Request, *args, **kwargs): """ 获取用户列表 """ return success_response()
     

    路由

     
    from django.conf.urls import url, include # 定义路由 from rest_framework.routers import SimpleRouter from apps.user import user_views router = SimpleRouter() router.register(r'test-user', user_views.UserViewSet, base_name='user')
     

    其他用法

     
    @list_route(methods=('post',), permission_classes=(permissions.IsAuthenticated,)) def chate(self, request):
      'base_url/users/chate/'
         pass
    @list_route(methods=('get',), permission_classes=(permissions.IsAuthenticated,)) def chat_current(self, request):
         
    'base_url/users/chate_current/'
         pass
  • 相关阅读:
    basic-linux
    巧用border属性
    git常用操作笔记
    如何删除github里的项目
    常用css3属性的ie兼容查看
    新建pc端页面的模板
    HTML5 Shiv--解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    进程和线程
    C++对象模型---第 4 章 Function语意学
    C++对象模型---第 3 章 Data语意学
  • 原文地址:https://www.cnblogs.com/ngngng/p/13745743.html
Copyright © 2011-2022 走看看