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')),
      ]
      
      
posted @ 2020-11-30 07:22  心-瘾  阅读(121)  评论(0编辑  收藏  举报