前言
一个表和多个表进行关联,但具体随着业务的加深,表不断的增加,关联的数量不断的增加,怎么通过一开始通过表的设计后,不在后期在修改表,彻底的解决这个问题呢呢
django中的一个组件content-type可以帮助我们解决这样的一个问题
在这里我先设计了3张表 学位表 普通课程 和价格策略表 大致的设计如下
在上图中我们可以看到价格策略表和其他的两个表进行了关联,可以根据表明
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')
进行插入数据的类视图
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('...')
为其添加路由
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)
输入以下的地址进行测试
http://127.0.0.1:8000/test
数据库中的结果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 大自然音乐系列《鲸声奇遇》[WAV/CUE/分轨][566.8MB]
- 陆虎.2024-时间是个有趣的东西【天娱传媒】【FLAC分轨】
- 江淑娜.1988-琼瑶原著电视连续剧主题曲【风格】【WAV+CUE】
- 蒋明.2015-空山【大象音乐】【FLAC分轨】
- 群星《2024好听新歌28》AI调整音效【WAV分轨】
- 发烧女声《不甘寂寞》[DTS-WAV分轨]
- 群星《2018新歌推荐》2CD/DTS[WAV分轨]
- BEYOND.2005-永远等待25周年限量版5CD【KINNS】【WAV+CUE】
- 高明骏.1993-透过你的双眼【巨冀】【WAV+CUE】
- 群星.1995-爱情,勿语【可登】【WAV+CUE】
- 大自然音乐系列《花漾物语》[WAV/CUE/分轨][473MB]
- 大自然音乐系列《轻风细雨》[WAV/CUE/分轨][568.3MB]
- 大自然音乐系列《听见向日葵的微笑》[WAV/CUE/分轨][538.5MB]
- 魔兽世界地心之战戒律牧用什么饰品 地心之战戒律牧饰品推荐
- 魔兽世界地心之战戒律牧用什么合剂食物 地心之战戒律牧合剂食物推荐