Django应用之content type(app应用之一django.contrib.contenttypes)
当一张表作为多个表的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)的更多相关文章
- Django Views: Dynamic Content
世味年来薄似纱,谁令骑马客京华. 小楼一夜听春雨,深巷明朝卖杏花. 矮纸斜行闲作草,晴窗细乳戏分茶. 素衣莫起风尘叹,犹及清明可到家. Your Second View: Dynamic Conten ...
- 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- ...
- Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...
- Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...
- pycharm上运行django服务器端、以及创建app方法
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 安装Django 下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py in ...
- SharePoint自动化系列——Add content type to list.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...
- SharePoint自动化系列——Content Type相关timer jobs一键执行
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- 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 ...
随机推荐
- python基础之知识补充-作用域、特殊语法
python作用域 无函数的作用域 在python中没有块级作用域 什么叫块级作用域呢?先来看个例子: if 1 == 1: name= 'alex' print(name) 运行结果为alex 在j ...
- nginx conf 文件
server { listen ; server_name local.light.com; index index.html index.htm index.php; root /home/wwwr ...
- 基于JOSE4J 实现的OAUTH TOKEN
jose4j / JWT Examples View History JSON Web Token (JWT) Code Examples Producing and consuming a sign ...
- 云计算第一章:服务器硬件及linux初体验
第一章:服务器硬件及linux初体验 一.了解linux: 1.linux的三大分支:debian.redhat.ubuntu 2.redhat版本: redhat:红帽,简称RHEL,企业级官方版本 ...
- Sping 补充完成修改功能
1.视图层完整示例 <form action="#" th:action="@{/update/{id}(id=${user.id})}" th:obje ...
- sas(Serial Attached SCSI)技术详解
1.什么是SAS 即串行SCSI技术,是一种磁盘连接技术,它综合了并行SCSI和串行连接技术(FC,SSA,IEEE1394)的优势,以串行通讯协议为协议基础架构,采用SCSI-3扩展指令集,并兼容S ...
- 从零开始学游戏开发(一):下载与安装UE4游戏引擎
如何下载和安装虚幻引擎 下载Epic Games Launcher 步骤 百度搜索"what is ue4" 点击第一个搜索结果,进入ue4官网 进入官网首页,点击右上角下载 创建 ...
- python 模块import(26)
一.模块简介 python开发中,每一个.py文件都可以看作是一个模块,模块内部的函数或者方法可以被其他模块调用,至于函数或者方法是具体如何实现的,调用者不需要关心. 假如项目中既含有UI也有逻辑代码 ...
- C++操作文件行(读取,删除,修改指定行)
/******************************************************** Copyright (C), 2016-2018, FileName: main A ...
- ACMComputerFactory(POJ-3436)【最大流】
题目链接:https://vjudge.net/problem/POJ-3436 题意:要用N台机器来组装电脑,每台电脑由P个零部件构成,每一台机器的输入电脑和输出电脑的每部分都有各自的属性,机器本身 ...