1, 先写一个APP, 名字叫dsj
1.1 urls.py文件内容如下:
from django.urls import path, re_path, include
from django.conf.urls import url
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(prefix=r'xxx', viewset=views.V3View)
router.register(prefix=r'rt', viewset=views.V3View)
urlpatterns = [
re_path(r'^roles/$', views.RolesView.as_view()),
re_path(r'^userinfo/$', views.UserInfoView.as_view()),
re_path(r'^group/(?P\d+)$', views.GroupView.as_view(), name='gp'),
re_path(r'^usergroup/$', views.UserGroupView.as_view()),
re_path(r'^page1/$', views.Page1View.as_view()),
re_path(r'^page2/$', views.Page1View_2.as_view()),
re_path(r'^page3/$', views.Page1View_3.as_view()),
re_path(r'^v1/$', views.V1View.as_view()),
re_path(r'^v2/$', views.V2View.as_view({'get': 'xxx'})),
re_path(r'^v3/$', views.V3View.as_view({'get': 'list', 'post':'create'})),
re_path(r'^v3/(?P\d+)/$', views.V3View.as_view({'get': 'retrieve', 'delete':'destroy', 'put':'update', 'patch':'partial_update'})),
url(r'^', include(router.urls)),
url(r'^test/$', views.TestView.as_view())
]
1.2 models.py 文件内容如下,新建数据表
from django.db import models
class UserInfo(models.Model):
user_type_choices = (
(1, '普通用户'),
(2, 'VIP客户'),
(3, 'SVIP')
)
user_type = models.IntegerField(choices=user_type_choices)
username = models.CharField(max_length=32, unique=True)
password = models.CharField(max_length=64)
group = models.ForeignKey(to='UserGroup', on_delete=models.CASCADE)
roles = models.ManyToManyField('Role')
class UserGroup(models.Model):
title = models.CharField(max_length=32)
class Role(models.Model):
title = models.CharField(max_length=32)
1.3 views.py 文件编写, 写serializer 序列化和反序列化
'''
路由系统主要是在urls.py中完成的,这里没有代码,在哪里设置了之后可以自动生成url
转战到urls.py文件察看.
'''