当一张表作为多个表的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. Yarn简单介绍及内存配置

    本文出自:http://blog.chinaunix.net/uid/28311809/abstract/1.html 在这篇博客中,主要介绍了Yarn对MRv1的改进,以及Yarn简单的内存配置和Y ...

  2. thymeleaf中double/float格式化,四舍五入显示两位小数

    private Float balance; 代码: <span class="A124_balance_num" th:text="${#numbers.form ...

  3. 【Leetcode_easy】671. Second Minimum Node In a Binary Tree

    problem 671. Second Minimum Node In a Binary Tree 参考 1. Leetcode_easy_671. Second Minimum Node In a ...

  4. iOS-Foundation各种NS

    1.1 NSRange NSRange range = NSMakeRange(2, 4);//location=2,len=4    NSString *str = @"i love oc ...

  5. iOS-NSNotification本地推送、远程推送

    //一个完整的通知一般包含3个属性: - (NSString *)name; // 通知的名称 - (id)object; // 通知发布者(是谁要发布通知) - (NSDictionary *)us ...

  6. 2019强网杯部分misc&web

    0x01 前言 前两天菜鸡+x和几个大哥算是正式参加了一次ctf的线上赛,也是第一次参加这种比赛(前一段时间巨佬也给了我们一个西班牙的比赛,不过不算是正式参赛,做题的时候,比赛已经结束了),没想到出师 ...

  7. Ext.jsTree 向子节点添加叶子节点

    // 定义搜索节点树结构Store const nodeStore = Ext.create('Ext.data.TreeStore', { autoLoad : true, id : 'nodeSt ...

  8. CF1227C Messy

    思路: 构造题.首先把字符串变成“(((((...)))))”的样子,再根据k的取值变成“()()()...((...))”的样子即可. 实现: #include <bits/stdc++.h& ...

  9. 最新 边锋网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.边锋网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了边锋网络.6.7月主要是做系统复习.项目复盘.Leet ...

  10. printf特殊用法

    printf("%*.*lf\n", a, b, c); //表示a宽距,保留b位小数 用这种方法可以通过输入控制a和b