Django組件content-type使用方法詳解
前言
一個表和多個表進(jìn)行關(guān)聯(lián),但具體隨著業(yè)務(wù)的加深,表不斷的增加,關(guān)聯(lián)的數(shù)量不斷的增加,怎么通過一開始通過表的設(shè)計(jì)后,不在后期在修改表,徹底的解決這個問題呢呢
django中的一個組件content-type可以幫助我們解決這樣的一個問題
在這里我先設(shè)計(jì)了3張表 學(xué)位表 普通課程 和價格策略表 大致的設(shè)計(jì)如下

在上圖中我們可以看到價格策略表和其他的兩個表進(jìn)行了關(guān)聯(lián),可以根據(jù)表明
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)
# 僅用于反向查找 不在數(shù)據(jù)庫中添加字段
price_policy_list = GenericRelation("PricePolicy")
class DegreeCourse(models.Model):
"""
學(xué)位課程
"""
title = models.CharField(max_length=32)
# 僅用于反向查找
price_policy_list = GenericRelation("PricePolicy")
class PricePolicy(models.Model):
"""
價格策略
"""
price = models.IntegerField()
period = models.IntegerField()
# 關(guān)聯(lián)表
content_type = models.ForeignKey(ContentType, verbose_name='關(guān)聯(lián)的表名稱') # 7,8 表名稱
object_id = models.IntegerField(verbose_name='關(guān)聯(lián)的表中的數(shù)據(jù)行的ID') #
# 幫助你快速實(shí)現(xiàn)content_type操作 ,快速插入數(shù)據(jù) 不生成數(shù)據(jù)庫中的字段
content_object = GenericForeignKey('content_type', 'object_id')
進(jìn)行插入數(shù)據(jù)的類視圖
from django.shortcuts import render,HttpResponse
from app01 import models
def test(request):
# 1. 為學(xué)位課“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. 為學(xué)位課“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. 根據(jù)課程ID獲取課程, 并獲取該課程的所有價格策略
# course = models.Course.objects.filter(id=1).first()
#
# price_policys = course.price_policy_list.all()
#
# print(price_policys)
return HttpResponse('...')
為其添加路由
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), ]
我們自己進(jìn)行插入數(shù)據(jù)可能會這樣寫
# 1. 為學(xué)位課“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)
輸入以下的地址進(jìn)行測試
http://127.0.0.1:8000/test
數(shù)據(jù)庫中的結(jié)果如下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)自動玩連連看的腳本分享
最近女朋友在玩連連看,玩了一個星期了還沒通關(guān),真的是菜。實(shí)在是看不過去了,直接用python寫了個腳本代碼可以自動玩連連看,感興趣的可以了解一下2022-04-04
對python中的float除法和整除法的實(shí)例詳解
今天小編就為大家分享一篇對python中的float除法和整除法的實(shí)例詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python使用scapy模塊實(shí)現(xiàn)ping掃描的過程詳解
這篇文章主要介紹了python使用scapy模塊實(shí)現(xiàn)ping掃描的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Python+Pandas實(shí)現(xiàn)數(shù)據(jù)透視表
對于數(shù)據(jù)透視表,相信對于Excel比較熟悉的小伙伴都知道如何使用它。本文將利用Python Pandas實(shí)現(xiàn)數(shù)據(jù)透視表功能,感興趣的可以學(xué)習(xí)一下2022-06-06

