| 一 项目背景 |
| 二 版本一 |
| 三 版本二 |
| 四 最终版(使用ContentType) |
Egon星球项目,有课程,学位课(不同的课程字段不一样),价格策略
问题,1 如何设计表结构,来表示这种规则
2 为专题课,添加三个价格策略
3 查询所有价格策略,并且显示对应的课程名称
4 通过课程id,获取课程信息和价格策略
一个课程表,包含学位课和专题课,一个价格策略表,一对多关联
学位课表,专题课表,装逼课表,价格策略表(在价格策略课表中加入多个FK跟课程表做关联):后期再加其它课程,可维护性差
通过Django提供的ContentType表,来构建
| 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) |
| |
| |
| |
| class DegreeCourse(models.Model): |
| title = models.CharField(max_length=32) |
| |
| class PricePolicy(models.Model): |
| |
| contentType = models.ForeignKey(to=ContentType) |
| |
| object_id = models.PositiveIntegerField() |
| |
| |
| |
| |
| period = models.CharField(max_length=32) |
| price = models.FloatField() |