Django組件content-type使用方法詳解


前言

  參考博客:https://www.zhangshengrong.com/p/zD1yQJwp1r/

  一個表和多個表進行關聯,但具體隨着業務的加深,表不斷的增加,關聯的數量不斷的增加,怎么通過一開始通過表的設計后,不在后期在修改表,徹底的解決這個問題呢?

django中的一個組件content-type可以幫助我們解決這樣的一個問題。在這里我先設計了3張表:學位表、 普通課程 和價格策略表 ,價格策略表和其他的兩個表進行了關聯,可以根據表content-type進行關聯。

  models.py

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType

class Course(models.Model):
  """
  普通課程
  """
  title = models.CharField(max_length=32)
  # 僅用於反向查找 不在數據庫中添加字段
  price_policy_list = GenericRelation("PricePolicy")

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

  # 僅用於反向查找
  price_policy_list = GenericRelation("PricePolicy")

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

  # 關聯表
  content_type = models.ForeignKey(ContentType, verbose_name='關聯的表名稱') # 7,8 表名稱
  object_id = models.IntegerField(verbose_name='關聯的表中的數據行的ID')  #
  # 幫助你快速實現content_type操作 ,快速插入數據 不生成數據庫中的字段
  content_object = GenericForeignKey('content_type', 'object_id')

  views.py進行插入數據的類視圖

from django.shortcuts import render,HttpResponse
from app01 import models
def test(request):

  # 1. 為學位課“Python全棧”添加一個價格策略:一個月 9.9
  # obj1 = models.DegreeCourse.objects.filter(title='Python全棧').first()
  # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
  #
  # obj2 = models.DegreeCourse.objects.filter(title='Python全棧').first()
  # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
  #
  # obj3 = models.DegreeCourse.objects.filter(title='Python全棧').first()
  # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)

  # 2. 為學位課“rest”添加一個價格策略:一個月 9.9
  # obj1 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1)
  #
  # obj2 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
  #
  # obj3 = models.Course.objects.filter(title='rest framework').first()
  # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3)

  # 3. 根據課程ID獲取課程, 並獲取該課程的所有價格策略
  # course = models.Course.objects.filter(id=1).first()
  #
  # price_policys = course.price_policy_list.all()
  #
  # print(price_policys)

  return HttpResponse('...')

  urls.py為其添加路由

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^test/', views.test),
]

  自行插入數據可能會這樣寫  

# 1. 為學位課“Python全棧”添加一個價格策略:一個月 9.9
"""
obj = DegreeCourse.objects.filter(title='Python全棧').first()
# obj.id
cobj = ContentType.objects.filter(model='course').first()
# cobj.id
PricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)
"""
# obj = DegreeCourse.objects.filter(title='Python全棧').first()
# PricePolicy.objects.create(price='9.9',period='30',content_object=obj)
手動插入方式

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM