程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Note: Django rest framework router

編輯:Python

1, Write a first APP, Name is dsj

1.1 urls.py The contents of the document are as follows :

from django.urls import path, re_path, include
from django.conf.urls import url
from . import views
# The following is to use rest framework The routing system is set 
from rest_framework import routers
router = routers.DefaultRouter() # Instantiate a default class 
router.register(prefix=r'xxx', viewset=views.V3View) # Register in the system , Note the parameters here ,prefix Prefix means , Generated url There will be this ,viewset Which view you want to register , Here is V3View
router.register(prefix=r'rt', viewset=views.V3View) # There's nothing wrong here , Because the prefix is different , Generated url Is different . Register in the system , Note the parameters here ,prefix Prefix means , Generated url There will be this ,viewset Which view you want to register , Here is V3View
# How to verify the above routing settings , And the following settings , As long as we are url Misspelled in , Then it will report many wrong routes , You can see whether there is an automatically generated route url
urlpatterns = [
re_path(r'^roles/$', views.RolesView.as_view()),
re_path(r'^userinfo/$', views.UserInfoView.as_view()),
re_path(r'^group/(?P<pk>\d+)$', views.GroupView.as_view(), name='gp'), # Notice the pk, It is written in the default system , Can be in view To get rid of ., The source code in : class HyperlinkedRelatedField(RelatedField): lookup_field = 'pk' This pk That's what people default on pk, however pk incorrect , because pk yes id,id It is changing. , Two groups are fixed .
re_path(r'^usergroup/$', views.UserGroupView.as_view()),
re_path(r'^page1/$', views.Page1View.as_view()), # This is when testing the paging function URL
re_path(r'^page2/$', views.Page1View_2.as_view()), # This is when testing the paging function URL
re_path(r'^page3/$', views.Page1View_3.as_view()), # This is when testing the paging function URL
re_path(r'^v1/$', views.V1View.as_view()), # This is the test restframework View View function URL
re_path(r'^v2/$', views.V2View.as_view({
'get': 'xxx'})), # here V2View() Inherited GenericViewSet, This class overrides as_view() Method , Delivery required action Method 
# here V3View() Inherited ModelViewSet, ModelViewSet And inherited GenericViewSet(), then GenericViewSet Rewrote as_view() Method , So our customized class view inherits ModelViewSet() class 
# Also rewrite url Medium as_view(), Pass him parameters .
# First of all, this one-to-one correspondence requires querying the source code , There are specific function names in the source code .
re_path(r'^v3/$', views.V3View.as_view({
'get': 'list', 'post':'create'})), # Pay attention to this url First half of , Didn't write that pk, What do you mean , Express get All the data is requested ,post The request does not need to be specified pk, You can directly pass the corresponding fields to the database ,
# Note the following url, This one's inside pk\d+ It refers to a data in the database , It is no longer a whole , Operate on a data , Look at the corresponding methods . Remember not to make mistakes . You can realize the function of adding, deleting, modifying and checking one by one 
re_path(r'^v3/(?P<pk>\d+)/$', views.V3View.as_view({
'get': 'retrieve', 'delete':'destroy', 'put':'update', 'patch':'partial_update'})),
#'''
# The following code is the routing system , Automatically generated routes 
# Why should the system automatically generate url, Because for a view , Tend to have 2 individual url, Because it is the reason to obtain the overall and individual data , Then add json Format reason , There will be one 
# The view generates 4 individual url, Then if you think it's troublesome to write , You can set some here , Let the system help you generate 
#'''
# url(r'(?P<version>)[v1|v2]/$', include(router.urls)), Writing with version prefix .
url(r'^', include(router.urls)), # You have to use it here url(),re_path and path no way .
# Below url It is used to test the renderer 
url(r'^test/$', views.TestView.as_view())
]

1.2 models.py The contents of the document are as follows , New data table

from django.db import models
class UserInfo(models.Model):
user_type_choices = (
(1, ' Ordinary users '),
(2, 'VIP Customer '),
(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) # Suppose a group corresponds to multiple people , One person only has multiple groups 
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 Documentation , Write serializer Serialization and deserialization

#################################### The routing system starts ##############################################################
''' The routing system is mainly in urls.py Done in , There's no code here , Where can it be automatically generated after setting url Transfer to urls.py Document viewing . '''
############## The routing system ends ####################

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved