当一张表作为多个表的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. building confluentinc kafka-connect-hdfs

    When I try to compile I get an error about a missing SNAPSHOT dependency. The error looks something ...

  2. python 运算符和小数据池

    计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算,今天我们暂只学习算数运算.比较运算.逻辑运算.赋值运算 算数运 ...

  3. keepalived两台机器同时出现vip问题

    配置文件: 主:192.168.1.14 ! Configuration File for keepalived global_defs { script_user root enable_scrip ...

  4. 分布式消息通信之RabbitMQ_Note

    目录 1. RabbitMQ 安装 2. RabbitMQ 应用场景,特性 3. 官网入门指引 4. RabbitMQ 工作模型 5. RabbitMQ 主要的几种交换机类型 6. Java API的 ...

  5. 【miscellaneous】 GStreamer应用开发手册学习笔记之基础概念介绍

    第3章. 基础概念介绍 本章将介绍GStreamer的基本概念. 理解这些概念对于你后续的学习非常重要,因为后续深入的讲解我们都假定你已经完全理解了这些概念. 3.1. 元件(Elements) 元件 ...

  6. ul根据后台添加li,并在点击li时,颜色随之变化

    在我们大多数时候都是通过<ul><li>...</li></ul>来实现同级的加载,但是也用很多时候li里的内容是不固定的.需要根据后台返回数据来生成. ...

  7. sklearn.feature_extraction.text.CountVectorizer 学习

    CountVectorizer: CountVectorizer可以将文本文档集合转换为token计数矩阵.(token可以理解成词) 此实现通过使用scipy.sparse.csr_matrix产生 ...

  8. MapReduce的输入文件是两个

    [学习笔记] 1.对于MapReduce程序,如何输入文件是两个文件? 这一小节,我们将继续第一章大数据入门的HelloWorld例子做进一步的研究.这里,我们研究如何输入文件是两个文件.packag ...

  9. Spring Boot系列教程十二:Spring boot集成Redis

    一.创建项目 项目名称为 "springboot_redis",创建过程中勾选 "Web","Redis",第一次创建Maven需要下载依赖 ...

  10. 数位dp踩坑

    前言 数位DP是什么?以前总觉得这个概念很高大上,最近闲的没事,学了一下发现确实挺神奇的. 从一道简单题说起 hdu 2089 "不要62" 一个数字,如果包含'4'或者'62', ...