django之contenttype組件

ContentType組件介紹java

  ContentType是Django的內置的一個組件,能夠追蹤項目中全部的APP和model的對應關係,並記錄在ContentType表中。跟其餘建表外鍵如(ForeignKey、OneToOneField等)同樣,實現表與表之間的聯繫。但ContentType與其餘外鍵不一樣的是,ContentType組件更強大,好比對錶與表之間混搭、適用於一張表同時和多張表關聯。django

ContentType組件的使用app

  1.引入django.contrib.contenttypes到settings.py的INSTALLED_APPS中url

  

  2.配置model  (這裏咱們定義普通課程、學位課程加上價格策略)spa

  使用contenttype時先導入模塊code

from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation        #GenericRelation:反向查詢
from django.contrib.contenttypes.models import ContentType

  而後是model中的內容對象

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation        #GenericRelation:反向查詢
from django.contrib.contenttypes.models import ContentType

# Create your models here.
class Course(models.Model):
    """
    普通課程
    """
    title = models.CharField(max_length=32)

    # 用於反向查詢到ReportRecord表, 不會生成表字段
    #reports = GenericRelation(to='PricePolicy')

class DegreeCourse(models.Model):
    """
    學位課程
    """
    title = models.CharField(max_length=32)

    # 用於反向查詢到ReportRecord表, 不會生成表字段
    # reports = GenericRelation(to='PricePolicy')

class PricePolicy(models.Model):
    """
    價格策略
    """
    price = models.IntegerField()
    period = models.IntegerField()

    content_type = models.ForeignKey(ContentType, max_length=32,on_delete=models.CASCADE)  # ContentTpye組件:把全部表名存放到ContentType組件的表中
    object_id = models.IntegerField(verbose_name='關聯的表中的數據行的ID')
    content_object = GenericForeignKey('content_type','object_id')

  3.遷移數據表blog

  makemigrations   migrateget

  

  之間已經遷移好了。這裏就沒有在生成了。it

  4.配置url(API)

from django.contrib import admin
from django.urls import path

from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('test/',views.TestView),
]

  5.配置View

from django.shortcuts import render,HttpResponse

from .models import PricePolicy,DegreeCourse,ContentType,Course
# Create your views here.

def TestView(request):
    #1.爲學位課「c」添加一個價格策略
    #方式一:   麻煩
    # GR = DegreeCourse.objects.filter(title='c')     #得到該課程的對象
    # price = PricePolicy.objects.get()
    # CT = ContentType.objects.get(model='degreecourse').first()  #得到該表名的對象
    # PricePolicy.objects.create(price='9.9',period='30',content_type=CT.id,object_id=GR)
    
    #方式二: 推薦
    CT = ContentType.objects.get(model='degreecourse')  #得到該表名的對象
    GR = DegreeCourse.objects.filter(title='c').first()    #得到該課程的對象
    PricePolicy.objects.create(price=9.9,period=30,content_type=CT,object_id=GR.id)

    CT1 = ContentType.objects.get(model='degreecourse') # 得到該表名的對象
    GR = DegreeCourse.objects.filter(title='java') .first()    #得到該課程的對象
    PricePolicy.objects.create(price=19.9, period=30, content_type=CT1,object_id=GR.id)

    CT2 = ContentType.objects.get(model='course')  # 得到該表名的對象
    GR = Course.objects.filter(title='c1').first()  # 得到該課程的對象
    PricePolicy.objects.create(price=19.9, period=30, content_type=CT2, object_id=GR.id)

    return HttpResponse('添加成功')

model各表分析:(注意 到這裏這個步驟只是數據遷移加上生成了model中的表,沒有執行程序)

  1.django_content_type表

    是ContentType組件自動生成的表,記錄當前的Django項目中全部model所屬的app(即app_label屬性)以及model的名字(即model屬性)

  

  2.Course表(記錄普通課程信息)

  

  3.DegreeCourse學位表(記錄全套課程信息)

  

結果分析:(運行程序後)

  在app01_pricepolicy表中添加了一下數據

  

  content_type_id是經過django_content_type的model屬性得到對應表名的ID(表明表的id)

  object_id經過表中的課程名得到課程的id(課程id)

 

綜上所述,我我的以爲ContentTypet挺強大的,比喻是一座橋樑(交通樞紐),令人或車能跨越河流順利同行

相關文章
相關標籤/搜索