Django组件content-type使用方法详解
框架(架构)  /  管理员 发布于 7年前   165
前言
一个表和多个表进行关联,但具体随着业务的加深,表不断的增加,关联的数量不断的增加,怎么通过一开始通过表的设计后,不在后期在修改表,彻底的解决这个问题呢呢
django中的一个组件content-type可以帮助我们解决这样的一个问题
在这里我先设计了3张表 学位表 普通课程 和价格策略表 大致的设计如下
在上图中我们可以看到价格策略表和其他的两个表进行了关联,可以根据表明
models.py
from django.db import modelsfrom django.contrib.contenttypes.fields import GenericForeignKey, GenericRelationfrom django.contrib.contenttypes.models import ContentTypeclass 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='关联的表名称') # 7,8 表名称 object_id = models.IntegerField(verbose_name='关联的表中的数据行的ID') # # 帮助你快速实现content_type操作 ,快速插入数据 不生成数据库中的字段 content_object = GenericForeignKey('content_type', 'object_id')
进行插入数据的类视图
from django.shortcuts import render,HttpResponsefrom app01 import modelsdef test(request): # 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9 # obj1 = models.DegreeCourse.objects.filter(title='Python全栈').first() # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1) # # obj2 = models.DegreeCourse.objects.filter(title='Python全栈').first() # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2) # # obj3 = models.DegreeCourse.objects.filter(title='Python全栈').first() # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # 2. 为学位课“rest”添加一个价格策略:一个月 9.9 # obj1 = models.Course.objects.filter(title='rest framework').first() # models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj1) # # obj2 = models.Course.objects.filter(title='rest framework').first() # models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2) # # obj3 = models.Course.objects.filter(title='rest framework').first() # models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # 3. 根据课程ID获取课程, 并获取该课程的所有价格策略 # course = models.Course.objects.filter(id=1).first() # # price_policys = course.price_policy_list.all() # # print(price_policys) return HttpResponse('...')
为其添加路由
from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', views.test),]
我们自己进行插入数据可能会这样写
# 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9"""obj = DegreeCourse.objects.filter(title='Python全栈').first()# obj.idcobj = ContentType.objects.filter(model='course').first()# cobj.idPricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)"""# obj = DegreeCourse.objects.filter(title='Python全栈').first()# PricePolicy.objects.create(price='9.9',period='30',content_object=obj)
输入以下的地址进行测试
http://127.0.0.1:8000/test
数据库中的结果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号