zoukankan      html  css  js  c++  java
  • Django rest-framework框架-content-type

    表结构讨论:

    是用一张表价格策略表来记录两种不同的价格策略

    content-type原理:

    使用一张表来记录不同课程的价目,增加一行表名称

    注释: 适用于多张表关联一张表的情况

     会自动生成这种的结构:

    content_type: Django内置的一个组件, 帮助开发者做连表操作
                  连表操作: 普通的又 ForeignKey,OneToOne, ManyToMany
                  高级连表操作: content_type
    
    
    第一版: 普通方式来生成这种表关联
    class Course(models.Model):
    	"""普通课"""
    	title = models.CharField(max_length=32)
    
    class DegreeCourse(models.Model):
    	"""学位课"""
    	title = models.CharField(max_length=32)
    
    class PricePolicy(models.Model):
    	"""价格策略"""
    	price = models.IntegerField()
    	period = models.IntegerField()
    
    	table_name = models.CharField(verbose_name="关联的表名称")
    	object_id = models.CharField(verbose_name="关联的表中的数据行ID")
    
    第二版: 利用contentype来做多表关联
    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()
        #关联到ContentType表 
    	table_name = models.ForeignKey(ContentType, verbose_name="关联普通课表或者学位课表")
    	object_id = models.IntegerField(verbose_name="关联普通课表或者学位课表中的数据行ID")
    
    	#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)
    	#以上操作用下面代替
    	content_object = GenericForeignKey('content_type', 'object_id')
    
    # 添加view例子
    
    def test(request):
    	#1. 为学位课PYTHON全栈 添加一个价格策略 一个月 9.9
    	obj1 = DegreeCourse.objects.filter(title="PYTHON全栈").first()
    	PricePolicy.objects.create(price=9.9,period='一个月',content_type=obj1)
    
    	obj2 = DegreeCourse.objects.filter(title="PYTHON全栈").first()
    	PricePolicy.objects.create(price=39.9,period='二个月',content_type=obj2)
    
    	obj3 = DegreeCourse.objects.filter(title="PYTHON全栈").first()
    	PricePolicy.objects.create(price=59.9,period='三个月',content_type=obj3)
    
    	#2. 根据课程ID获取课程, 并获取该课程的所有价格策略
    	course = models.Course.objects.filter(id=1).first()
    	price_policys = course.price_policy_list.all()
    
    	return HttpResponse("添加成功")
    

      

  • 相关阅读:
    iOS 记录近期遇到的几个bug
    iOS转场动画封装
    iOS 页面滑动与标题切换颜色渐变的联动效果
    Realm数据持久化方案的简单介绍和使用(二)
    某互联网科技公司的一道逻辑面试题
    js获取本月,本季度,上个季度,本周,上周的起始和结束时间
    CSS样式说明
    a标签的样式
    CSS3的> 和@
    CSS中的position定位
  • 原文地址:https://www.cnblogs.com/kuku0223/p/11353763.html
Copyright © 2011-2022 走看看