zoukankan      html  css  js  c++  java
  • django 角色模板 后端接口 modelviewset

    django 后端接口 modelviewset

    1. apps/user/models.py

      1. # 中文角色名称
        class Role(models.Model):
            zh_name = models.CharField('中文角色名称', max_length=32)
            name = models.CharField('角色名称', max_length=32)
            description = models.TextField('描述')
        
            class Meta:
                db_table = '角色名称表'
                verbose_name = "角色名称表"
                verbose_name_plural = verbose_name
        
        
        
    2. serializers.py

      1. # 角色
        class RoleModelSerializer(serializers.ModelSerializer):
            class Meta:
                model = Role
                fields = '__all__'
        
        
    3. views.py

      1. # 角色
        # Create your views here.
        class RoleModelViewSet(ModelViewSet):
            queryset = Role.objects.all()
            serializer_class = RoleModelSerializer
            filter_backends = (DjangoFilterBackend, OrderingFilter)  # 同时支持过滤和排序
            ordering_fields = ('date_joined', 'id')  # ?ordering=-id
            # # 5.2指定过滤字段, 不设置, 过滤功能不起效
            filter_fields = ('zh_name', 'name')  # ?username = tom & phone = & is_active = true
        
        
        
    4. urls.py

      1. from django.urls import path, include
        from rest_framework.routers import DefaultRouter
        from .views import *
        
        router = DefaultRouter()
        router.register(r'user', UserModelViewSet)
        router.register(r'role', RoleModelViewSet))
        urlpatterns = [
            ]
        urlpatterns += router.urls
        
    5. 主路由

      1. """opwf URL Configuration
        
        The `urlpatterns` list routes URLs to views. For more information please see:
            https://docs.djangoproject.com/en/2.2/topics/http/urls/
        Examples:
        Function views
            1. Add an import:  from my_app import views
            2. Add a URL to urlpatterns:  path('', views.home, name='home')
        Class-based views
            1. Add an import:  from other_app.views import Home
            2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
        Including another URLconf
            1. Import the include() function: from django.urls import include, path
            2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
        """
        from django.contrib import admin
        from django.urls import path, include
        
        urlpatterns = [
            path('admin/', admin.site.urls),
            path('workflow/', include('workflow.urls')),
        ]
        
        
  • 相关阅读:
    了解HDD或SDD磁盘的健康状态
    修复丢失的打开方式
    Invoke-WebRequest : 请求被中止: 未能创建 SSL/TLS 安全通道。
    绕过禁止未登陆用户访问
    debug
    更新已有数据
    编码格式(乱码)
    ajax
    Http
    科学的管理和规范标准
  • 原文地址:https://www.cnblogs.com/wyx-zy/p/14059290.html
Copyright © 2011-2022 走看看