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="关联的表名称")
object_id = models.IntegerField(verbose_name="关联的表中的数据行ID")
content_obj = GenericForeignKey('content_type', 'object_id')
#查找价格策略
course = Course.objects.filter(id=1).first()
price_policy = course.price_policy_list.all()
#快速插入数据
obj1 = Course.objects.filter(title='Python').first()
PricePolicy.objects.create(price=9.9, period=7, content_type=obj1)
来源:oschina
链接:https://my.oschina.net/u/4397893/blog/4089302