Django組件content-type使用方法詳解
前言
一個(gè)表和多個(gè)表進(jìn)行關(guān)聯(lián),但具體隨著業(yè)務(wù)的加深,表不斷的增加,關(guān)聯(lián)的數(shù)量不斷的增加,怎么通過(guò)一開(kāi)始通過(guò)表的設(shè)計(jì)后,不在后期在修改表,徹底的解決這個(gè)問(wèn)題呢呢
django中的一個(gè)組件content-type可以幫助我們解決這樣的一個(gè)問(wèn)題
在這里我先設(shè)計(jì)了3張表 學(xué)位表 普通課程 和價(jià)格策略表 大致的設(shè)計(jì)如下
在上圖中我們可以看到價(jià)格策略表和其他的兩個(gè)表進(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ù)庫(kù)中添加字段 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): """ 價(jià)格策略 """ 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ù)庫(kù)中的字段 content_object = GenericForeignKey('content_type', 'object_id')
進(jìn)行插入數(shù)據(jù)的類(lèi)視圖
from django.shortcuts import render,HttpResponse from app01 import models def test(request): # 1. 為學(xué)位課“Python全?!碧砑右粋€(gè)價(jià)格策略:一個(gè)月 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”添加一個(gè)價(jià)格策略:一個(gè)月 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獲取課程, 并獲取該課程的所有價(jià)格策略 # 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ù)可能會(huì)這樣寫(xiě)
# 1. 為學(xué)位課“Python全?!碧砑右粋€(gè)價(jià)格策略:一個(gè)月 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)行測(cè)試
http://127.0.0.1:8000/test
數(shù)據(jù)庫(kù)中的結(jié)果如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例
今天小編就為大家分享一篇tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python定時(shí)庫(kù)APScheduler的原理以及用法示例
APScheduler的全稱是Advanced Python Scheduler,它是一個(gè)輕量級(jí)的 Python 定時(shí)任務(wù)調(diào)度框架,下面這篇文章主要給大家介紹了關(guān)于Python定時(shí)庫(kù)APScheduler的原理以及用法的相關(guān)資料,需要的朋友可以參考下2021-12-12Python中使用插入排序算法的簡(jiǎn)單分析與代碼示例
這篇文章主要介紹了Python使用插入排序算法的簡(jiǎn)單分析與代碼示例,插入算法的平均時(shí)間復(fù)雜度為O(n^2),需要的朋友可以參考下2016-05-05python實(shí)現(xiàn)處理Excel表格超詳細(xì)系列
這篇文章主要介紹了python實(shí)現(xiàn)處理Excel表格超詳細(xì)系列,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08Python?實(shí)現(xiàn)圖像特效中的油畫(huà)效果
這篇文章主要是為大家簡(jiǎn)單介紹一下圖像添加油畫(huà)特效的基本原理以及代碼實(shí)現(xiàn),文中的示例代碼很詳細(xì),對(duì)我們學(xué)習(xí)或者工作有一點(diǎn)的價(jià)值,感興趣的小伙伴可以了解一下2021-12-12新建文件時(shí)Pycharm中自動(dòng)設(shè)置頭部模板信息的方法
這篇文章主要介紹了新建文件時(shí)Pycharm中自動(dòng)設(shè)置頭部模板信息的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04python 實(shí)現(xiàn)簡(jiǎn)單的FTP程序
這篇文章主要介紹了python 實(shí)現(xiàn)簡(jiǎn)單的FTP程序,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Django用戶注冊(cè)并自動(dòng)關(guān)聯(lián)到某數(shù)據(jù)表?xiàng)l目的實(shí)現(xiàn)步驟
當(dāng)一個(gè)新用戶注冊(cè)并且你想要自動(dòng)關(guān)聯(lián)到特定的Box條目(假設(shè)其ID為1)時(shí),下面給大家分享完整實(shí)現(xiàn)流程和步驟,對(duì)Django關(guān)聯(lián)數(shù)據(jù)表?xiàng)l目實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧2017-04-04