1、django组件:contenttype

组件的作用:可以通过两个字段让表和N张表创建FK关系

1、专题课,学位课 如何关联 过期时间??

方法1:分别创建 专题课--过期时间表 、学位课--过期时间表

方法2:专题课,学位课对应到一张过期时间表

方法3:过期时间表与 学位课,专业课,or 其他课程,创建外键关系

2、models初始表结构

$ python manage.py makemigrations
$ python manage.py migrate

3、contenttype表

2、需求1:插入课程与过期时间

1、方法1:ContentType

2、方法2:GenericForeignKey

models表结构

不会生成数据库列

view视图

3、需求2 :根据价格策略查找对应的表和数据

4、需求3:查询过期时间和价格

5、contenttype组件的3件事

1张表跟多张表,动态的创建FK关系

2列实现多张表的FK

其他应用:

  优惠券跟多张课程进行关联

  公共评论表,与多张表进行关联

6、代码

django组件:contenttype
组件的作用:可以通过两个字段让表和N张表创建FK关系

models

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation class DegreeCourse(models.Model):
"""学位课程"""
name = models.CharField(max_length=128, unique=True)
course_img = models.CharField(max_length=255, verbose_name="缩略图")
brief = models.TextField(verbose_name="学位课程简介") class Course(models.Model):
"""专题课程"""
name = models.CharField(max_length=128, unique=True)
course_img = models.CharField(max_length=255) # 不会在数据库生成列,只用于帮助你进行添加和查询
policy_list = GenericRelation('PricePolicy') class PricePolicy(models.Model):
"""价格与课程有效期表"""
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) # 关联course or degree_course
object_id = models.PositiveIntegerField() # 不会在数据库生成列,只用于帮助你进行添加和查询
content_obj = GenericForeignKey('content_type','object_id') valid_period_choices = (
(1, '1天'),
(3, '3天'),
(7, '1周'),
(14, '2周'),
(30, '1个月'),
(60, '2个月'),
(90, '3个月'),
(180, '6个月'),
(360, '12个月'),
(540, '18个月'),
(720, '24个月'),
)
valid_period = models.SmallIntegerField(choices=valid_period_choices)
price = models.FloatField()

views

from django.shortcuts import render, HttpResponse
from django.contrib.contenttypes.models import ContentType
from app01 import models def test(request):
"""价格策略表中增加,查询"""
# 1.在价格策略表中添加一条数据
# 方法1
"""
models.PricePolicy.objects.create(
valid_period=7,
price=6.6,
content_type=ContentType.objects.get(model='course'),
object_id=1
)
""" # 方法2
"""
models.PricePolicy.objects.create(
valid_period=14,
price=9.9,
content_obj=models.Course.objects.get(id=1)
)
""" # 2.根据某个价格策略对象,找到他对应的表和数据,如:管理课程名称
'''
price = models.PricePolicy.objects.get(id=2)
print(price.content_obj.name) # 自动帮你找到
''' # 3、找到某个课程的所有价格策略
obj = models.Course.objects.get(id=1)
print(obj.policy_list.all()) for item in obj.policy_list.all():
print(item.id, item.valid_period, item.price) return HttpResponse('test...')

最新文章

  1. GridView中实现DropDownList联动
  2. Android自定义控件4--优酷菜单的菜单键及细节补充
  3. iframe子页面点击按钮,执行父页面的点击事件
  4. mysql5.7碰到的坑
  5. foremost
  6. Python之编写函数
  7. PHP自动加载__autoload的工作机制
  8. Eclipse控制台中文乱码
  9. NLS_LANG SIMPLIFIED CHINESE_CHINA.AL32UTF8 和american_america.AL32UTF8
  10. Bitmap recycle()
  11. Android Studio的使用(五)--导入第三方Jar包
  12. 15套java架构师、集群、高可用、高可扩 展、高性能、高并发、性能优化Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程
  13. 我的Chrome
  14. respondsToSelector
  15. java web后台工作原理
  16. [python] [Jupyter Notebook]
  17. MySQL 排名、分组后组内排名、取各组的前几名 及排名后更新插入数据表中
  18. 关于UIPageViewController那些事
  19. 怎么点击div之外的区域就隐藏这个div啊 找了很久,都没有很好解决
  20. python-day9-循环嵌套

热门文章

  1. linux_mysql学习系列
  2. Python+requests维持会话
  3. css 颜色自动变化 炫彩
  4. lxml and 代理ip
  5. 内存溢出,内存泄漏,CPU溢出区别
  6. ingress安装配置
  7. 分布式session一致性问题
  8. hdu 2189还是dp..
  9. 检索 COM 类工厂中 CLSID 为 {13C28AD0-F195-4319-B7D7-A1BDAA329FB8} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。
  10. centos 中 Java环境变量配置