当一张表作为多个表的FK(主键),并且只能选择其中一个或者几个时,就可以使用content_type表;例如下图的数据关系,因此就可以使用content_type表来将表与表中的对象进行关联,从而做到不增加列字段的情况下增加FK关系。

  在使用content_type表时,需要在FK表中增加content_type作为FK字段,并增加GenericForeignKey便于价格策略表记录的建立以及对应的课程的查找。

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation
class degreecourse(models.Model):
title = models.CharField(max_length=32)
#仅用于反向查找
price_1 = GenericRelation(to='Price_1') class course(models.Model):
title = models.CharField(max_length=32) class Price_1(models.Model):
price= models.IntegerField()
period = models.IntegerField()
content_type = models.ForeignKey(ContentType,verbose_name='...')
object_id = models.IntegerField()
content_list = GenericForeignKey('content_type','object_id')

--- 对应的模型类

def table(request):
#插入
# obj = degreecourse.objects.get(title='python')
# Price_1.objects.create(price=15,period=30,content_list=obj)
#
# obj = degreecourse.objects.get(title='python')
# Price_1.objects.create(price=20, period=60, content_list=obj)
#
# obj = degreecourse.objects.get(title='python')
# Price_1.objects.create(price=25, period=90, content_list=obj) # 查找
obj = degreecourse.objects.get(id=1)
#print(obj)
print(len(obj.price_1.all()))
return HttpResponse('xxx')

--对应的视图类

网上虽然有很多讲这个的,但是,当我用的时候,一边看着别人的,码着自己的,然后就发现了其中的很多错误,

这里有个我踩过的坑,就是这个

【content_type,object_id,不能使用其他变量来做更换】

【如果有做变换,就报错(Cannot resolve keyword 'content_type' into field. Choices are: content_list, content_type1, content_type1_id, id, object_id, period, price),如果有类似的报错,就需要注意是否变量名书写错误】,

暂时还没看内部源码实现过程,之后再慢慢补源码过程

Django应用之content type(app应用之一django.contrib.contenttypes)的更多相关文章

  1. Django Views: Dynamic Content

    世味年来薄似纱,谁令骑马客京华. 小楼一夜听春雨,深巷明朝卖杏花. 矮纸斜行闲作草,晴窗细乳戏分茶. 素衣莫起风尘叹,犹及清明可到家. Your Second View: Dynamic Conten ...

  2. Element 'dependencies' cannot have character[children],because the type's content type is elemen

    问题描述: Element 'xxxxxxx' cannot have character [children],because the type's content type is element- ...

  3. Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...

  4. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  5. pycharm上运行django服务器端、以及创建app方法

     快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 安装Django  下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py in ...

  6. SharePoint自动化系列——Add content type to list.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...

  7. SharePoint自动化系列——Content Type相关timer jobs一键执行

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...

  8. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  9. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

随机推荐

  1. 第一个php文件运行

    运行会发现报错,解决参考:写的很详细 http://blog.csdn.net/meegomeego/article/details/36020553

  2. 【pep8规范】arc diff 不符合 pep 8 规范

    arc land 的时候,arc报错提示代码不符合pep8规范: 1.单行代码过长(括号中的换行不需要加 /) python代码换行加 / https://blog.csdn.net/codechel ...

  3. 【web 安全测试Tools】BurpSuite 1.7.32及注册机【无后门版】

    BurpSuite 1.7.32 原版+注册机 下载 链接:https://pan.baidu.com/s/1LFpXn2ulTLlcYZHG5jEjyw 密码:mie3 注意无后门版文件完整性: b ...

  4. Flutter 弹出键盘屏幕溢出问题

    在使用输入框获取焦点,弹出键盘的时候,会导致屏幕溢出, 解决办法: resizeToAvoidBottomPadding: false, //输入框抵住键盘 return Scaffold( appB ...

  5. 已经安装了VRay但3dmax的材质编辑器里没有VRay材质的解决过程

    已经安装了VRay但3dmax的材质编辑器里没有VRay材质怎么办? 众所周知,vray是一款很好用的渲染器,但是安装过程和使用当中总会出现各种问题.昨天我就遇到了,捣鼓半天终于解决,分享给大家自己的 ...

  6. 安装新版redis4.0.6

    看看新版本有那些特性提升,测试用!先安装 网址:https://redis.io/download 获取:wget http://download.redis.io/releases/redis-4. ...

  7. 怎么通过外网来访问自己在Tomcat服务器中配置的项目

    目前还没有试验过 https://blog.csdn.net/qingyisuo/article/details/80086105

  8. js修改css属性值

    推荐用dom.style.setProperty('属性','属性值'); 例如: $("#id")[0].style.setProperty('margin-top','1px' ...

  9. # VsCode 配置C++调试运行

    VsCode 配置C++调试运行 打开命令面板快捷键为F1,软件上写的Ctrl+Shift+P似乎没用 先安装插件使得可以运行 先自行在vsc扩展中搜索C++安装C/C++插件 再参考知乎专栏中安装c ...

  10. laravle6.0-IOC-DI浅谈

    1.什么是IOC,DI IOC(Inversion of Control)控制反转:ioc意味着,你将自己设计好的对象交给容器来控制,而不是传统的在你的对象内部直接控制.比如: 人 操控 手机 做一些 ...