一 项目背景

路飞学成项目,有课程,学位课(不同的课程字段不一样),价格策略

问题:

​ 1 如何设计表结构,来表示这种规则

​ 2 为专题课,添加三个价格策略

​ 3 查询所有价格策略,并且显示对应的课程名称

​ 4 通过课程id,获取课程信息和价格策略

二 版本一

一个课程表,包含学位课和专题课,一个价格策略表,一对多关联

三 版本二

学位课表,专题课表,装逼课表,价格策略表(在价格策略课表中加入多个FK跟课程表做关联):后期再加其它课程,可维护性差

四 最终版(使用ContentType)

通过Django提供的ContentType表,来构建

models层创建:

from django.db import models

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation class Course(models.Model):
title = models.CharField(max_length=32)
# 不会在数据库中生成字段,只用于数据库操作
# policy = GenericRelation('PricePolicy',object_id_field='object_id',content_type_field='contentType') class DegreeCourse(models.Model):
title = models.CharField(max_length=32) class PricePolicy(models.Model):
# 跟ContentType表做外键关联
contentType = models.ForeignKey(to=ContentType)
# 正数
object_id = models.PositiveIntegerField() # 引入一个字段,不会在数据库中创建,只用来做数据库操作
# content_obj = GenericForeignKey('contentType', 'object_id') period = models.CharField(max_length=32)
price = models.FloatField()

views层:

from app01 import models
def test(request):
import json
# 方式一插入价格规则
# ret=models.ContentType.objects.filter(model='course').first()
# course=models.Course.objects.filter(pk=1).first()
# print(ret.id)
# models.PricePolicy.objects.create(period='30',price=100,object_id=course.id,contentType_id=ret.id) # 方式二插入价格规则
# course=models.Course.objects.filter(pk=1).first()
# # content_obj=course 会自动的把课程id放到object_id上,并且去ContentType表中查询课程表的id,放到contentType上
# models.PricePolicy.objects.create(period='60',price=800,content_obj=course)
# 增加学位课,价格规则
# degreecourse = models.DegreeCourse.objects.filter(pk=1).first()
# models.PricePolicy.objects.create(period='60', price=800, content_obj=degreecourse) # 查询所有价格策略,并且显示对应的课程名称
# ret=models.PricePolicy.objects.all()
# for i in ret:
# print(i.price)
# print(i.period)
# # content_obj 就是代指关联的课程,或者学位课程的那个对象
# print(type(i.content_obj))
# print(i.content_obj.title) # 通过课程id,获取课程信息和价格策略
course=models.Course.objects.filter(pk=1).first()
print(course.policy.all()) return render(request,'test.html')

最新文章

  1. GeoIP Legacy City数据库安装说明
  2. 十进制(decimal system)转换函数说明
  3. Spring 4.x Task 和 Schedule 概述(代java配置)
  4. instanceof关键字
  5. SqlServer2012 数据库的同步之SQL JOB + 建立链接服务器
  6. 淘宝JAVA中间件Diamond详解(2)-原理介绍
  7. OAuth 的权限问题与信息隐忧
  8. FFmpeg缩放swscale详解 <转>
  9. windows phone中,将crash report记录下来,写入文件,方便分析
  10. Swift - 使用UI Dynamics给UIKit组件添加移动吸附行为
  11. 【Android】error opening trace file: No such file or directory (2)
  12. LinkedHashMap简明
  13. 运维rpm语法
  14. C语言中关键字restrict的概念,使用范围,例子
  15. C#设计模式之十八状态模式(State Pattern)【行为型】
  16. 22.1 、react生命周期(一)
  17. Centos7安装vsftpd
  18. 内存数据库-H2简介与实践
  19. C中级 - 文件辅助操作
  20. jquery居中窗口-页面加载直接居中

热门文章

  1. 使用Cadence绘制PCB流程
  2. 文档生成工具——Doxygen
  3. JavaScript利用函数反转数组
  4. JVM系列【2】Class文件结构
  5. day08 Pyhton学习
  6. CentOS 7基础命令介绍
  7. 【不知道怎么分类】NOIP2016 蚯蚓
  8. docker系统化学习图文教程
  9. nginx安全:修改对外的服务软件名称并隐藏版本号(nginx1.18.0)
  10. python 操作conf配置文件方法