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

Note: Django rest framework content type

編輯:Python

1, Write a first APP, Name is dsj

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

"""content_type URL Configuration """
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('testview/', views.TestView.as_view())
]

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

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
class Course(models.Model):
''' General courses '''
title = models.CharField(max_length=32)
class DegreeCourse(models.Model):
''' Degree programs '''
title = models.CharField(max_length=32)
class PricePolicy(models.Model):
''' Price strategy table '''
price = models.FloatField()
period = models.IntegerField()
# # Here's the point :
# # This is our handwritten implementation , We can also use systematic content_type To achieve .
# # Pay attention to this table_name Is the name of other tables we store in this table , Remember the name of the table , Not the fields of the table , Is the name of other tables , Fill in the fields of this form .
# table_name = models.CharField(verbose_name=" Associated table name ")
#
# object_id = models.CharField(verbose_name=" Of the data rows in the associated table ID")
# The following is provided by the system content_type To achieve 
# Pay attention to the inside of this foreign key ContentType, This is actually a table in the system ,, What is stored in this table is our whole project The names of all the tables generated .
# To put it simply ContentType Is a table , What is stored in this table is All tables of the whole project , His field is the table name .
# So here is to put this PricePolicy Inside the watch table_name Field link to ContentType surface , And then index contenttype surface , You can find the name of the table we want to store , Then add, delete, modify and check 
# In fact, that is ContentType It's a data sheet with God's perspective , You can link to him , And then through him , Query other tables . Equivalent to one The color of the middleman .
table_name = models.ForeignKey(ContentType, verbose_name=' Associated table name ',on_delete=models.CASCADE)
object_id = models.IntegerField(verbose_name=" Of the data rows in the associated table ID")
# To help you express the link ContentType surface , We also want to add a sentence here 
# Link the above two sentences .
content_object = GenericForeignKey('table_name', 'object_id')

pricepolicy Table structure

content_type Table structure , System native , Open the view of God's perspective .

1.3 views.py Documentation

from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Course, DegreeCourse, PricePolicy
from django.contrib.contenttypes.models import ContentType
class TestView(APIView):
def get(self,request, *args, **kwargs):
#1, For degree courses python Total station , Add a price strategy , Strategy is : A month 9.9 element 
# Here is what I wrote , But it's wrong ,
# Get the course first id
obj = DegreeCourse.objects.filter(title='python Whole station course ').first()
# First get the table name 
cobj = ContentType.objects.filter(model='degreecourse').first()
# Then put the above value below .
PricePolicy.objects.create(price=9.9, period=301, table_name_id=cobj.id, object_id=obj.id)
# Better way 
obj_d1 = DegreeCourse.objects.filter (title='python Whole station course ').first ()
PricePolicy.objects.create(price=9.9, period=30, content_object=obj_d1)
obj_d2 = DegreeCourse.objects.filter (title='python Whole station course ').first ()
PricePolicy.objects.create(price=19.9, period=60, content_object=obj_d2)
obj_d3 = DegreeCourse.objects.filter (title='python Whole station course ').first ()
PricePolicy.objects.create(price=29.9, period=90, content_object=obj_d3)
obj_d4 = DegreeCourse.objects.filter (title='python Whole station course ').first ()
PricePolicy.objects.create(price=39.9, period=120, content_object=obj_d4)
# Better way 2
obj_d1 = Course.objects.filter (title=' Small course ').first ()
PricePolicy.objects.create (price=8.8, period=30, content_object=obj_d1)
obj_d2 = Course.objects.filter (title=' Small course ').first ()
PricePolicy.objects.create (price=18.8, period=60, content_object=obj_d2)
obj_d3 = Course.objects.filter (title=' Small course ').first ()
PricePolicy.objects.create (price=28.8, period=90, content_object=obj_d3)
obj_d4 = Course.objects.filter (title=' Small course ').first ()
PricePolicy.objects.create (price=38.8, period=120, content_object=obj_d4)
return Response('xx')

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