content_type
1.作用
将app名称与其中表关系进行保存
在models创建表时,关联到ContentType并不会产生实际的字段
2.使用
在models中代码
from django.db import models
from django.contrib.contenttypes.fields import GenericRelation
# Create your models here.
class Courser(models.Model):
title = models.CharField(max_length=32)
#这里的object_id_field有默认值object_id,content_type_field有默认值content_type
#如果PricePolicy的字段名不是和默认值一样,那就要和下面一样,和PricePolicy表里的字段名对上
#不需要做数据库迁移,不会生成字段,只是方便用来查询
policy = GenericRelation('PricePolicy', object_id_field='course_id', content_type_field='table_id')
def __str__(self):
return self.title
class DegreeCourser(models.Model):
title = models.CharField(max_length=32)
policy = GenericRelation('PricePolicy', object_id_field='course_id', content_type_field='table_id')
def __str__(self):
return self.title
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
class PricePolicy(models.Model):
price = models.DecimalField(max_digits=8, decimal_places=2)
period = models.CharField(max_length=32)
# 强调,如果是外部导入的表,不能带引号
# 表的id
table_id = models.ForeignKey(to=ContentType)
# 课程id
course_id = models.IntegerField()
# PositiveIntegerField()---正整数
# 如果表id字段名叫:content_type,课程id字段名叫:object_id,GenericForeignKey()就不需要传参
# 不需要做数据库迁移,也不会在数据库生成字段,只用来查询和插入
# 如果保存的时候,只需要传content_obj这个字段,内部自动会保存table_id,course_id
content_obj = GenericForeignKey('table_id', 'course_id')
在views中代码
from django.shortcuts import render,HttpResponse,reverse,redirect
from django.contrib.contenttypes.models import ContentType
# Create your views here.
from app01 import models
def test(request):
#为django专题课添加三个价格策略
# course=models.Courser.objects.get(pk=1)
#
# table_id=ContentType.objects.get(model='courser')
# ret=models.PricePolicy.objects.create(price=9.9,period='1个月',table_id=table_id,course_id=course.pk)
# ret=models.PricePolicy.objects.create(price=9.9,period='2个月',table_id=table_id,course_id=course.pk)
# ret=models.PricePolicy.objects.create(price=9.9,period='3个月',table_id=table_id,course_id=course.pk)
#使用contenttype插入,其实就是根据course,查出course.pk,course所在表的表名,本质上还是做了上面的操作
# course = models.Courser.objects.get(pk=2)
# ret=models.PricePolicy.objects.create(price=9.9,period='1个月',content_obj=course)
# ret=models.PricePolicy.objects.create(price=19.9,period='2个月',content_obj=course)
# ret=models.PricePolicy.objects.create(price=29.9,period='3个月',content_obj=course)
#
#给学位课加一个价格策略
# degre_course=models.DegreeCourser.objects.get(pk=1)
# models.PricePolicy.objects.create(price=19999.9,period='5个月',content_obj=degre_course)
#查询所有价格策略,并且显示对应的课程名称
# ret=models.PricePolicy.objects.all()
# for i in ret:
# print(type(i.content_obj))
# print(i.content_obj)
#查询Django所有的价格策略
course=models.Courser.objects.get(pk=1)
course_policy=course.policy.all()
for i in course_policy:
print(i.period)
return HttpResponse('ok')
content_type的更多相关文章
- Django content_type 简介及其应用
在网上看到 django ORM 有一个 content_type 字段表的应用,这张表不是我们通过建立model类添加的,而是django自动帮我们生成的,具体的作用先简单的举个例子给大家介绍一下. ...
- $Django content_type组件 缓存组件
1 content_type组件(只能方便插入添加) 需求:课程,学位课(不同的课程字段不一样),价格策略 #免费课 class Free_classes (models.Model): id = ...
- AI-跨域、垃圾回收、content_type组见、接口处理
AI-跨域.垃圾回收.content_type组见.接口处理 跨域 为什么有跨域?什么时候遇见的?答:由于浏览器的同源策略 阻止ajax请求 不阻止src请求:在测试时,项目上线后不会遇见跨域.源:协 ...
- 11.8Django中的组件content_type
2018-11-8 18:59:11 在Django中已经有一个contenttype这个组件,并且在python manage.py makemigrations 和migrate的时候,一起在数据 ...
- django之content_type
什么是content type:django内置的一个组件,这个组件帮忙做连表的操作.(混搭连表) 适用场景:适用于一张表与多张表同时做关联的时候.直接导入就可以使用了. 关联数据库说有的表:让我们可 ...
- Django 组件content_type
content type: django内置组件,这个组件帮忙做连表操作(混搭连表) 适用场景:适用于一张表与多张表同时做关联的时候.直接导入就可以使用了. 关联数据库所有的表:可以快速插入数据,并且 ...
- django model content_type 使用
一.关于content_type 使用 1.引入模块在models from django.db import models from django.contrib.contenttypes.mode ...
- 11.关于django的content_type表
****** Django的contenttype表中存放发的是app名称和模型的对应关系 contentType使用方式 - 导入模块 from django.contrib.contenttype ...
- Django中的content_type表
models.py from django.db import models from django.contrib.contenttypes.models import ContentType # ...
随机推荐
- OpenGL——二维几何变换
平移.旋转.缩放的实现 #include<iostream> #include <math.h> #include<Windows.h> #include < ...
- Android开发训练之第五章第七节——Transmitting Network Data Using Volley
Transmitting Network Data Using Volley GET STARTED DEPENDENCIES AND PREREQUISITES Android 1.6 (API L ...
- mybatis与hibernate区别与应用场景
hibernate:是一个标准化的ORM框架.入门的门槛较高,不需要程序写sql,语句就自动生成了.对sql进行优化.修改比较困难. 应用场景:适用于中小企业需求变化不多的项目,比如后台管理系统,er ...
- D - Brave Game
十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中的部分电脑特技印象深刻. 今天,大家选择 ...
- B - 取(2堆)石子游戏
有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后把石子全部取完者为胜者. ...
- day9 九、函数
一.函数 1.函数:可以完成特定功能的代码块,函数就是存放代码块的容器 2.定义函数的语法: 函数四部分:(函数执行的本质:执行函数体,得到函数返回值) ①函数名 ②函数体 ③返回值 ④参数 用def ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- [No0000161]IDEA初步接触
安装 参考https://blog.csdn.net/qq_35246620/article/details/61191375 安装过程全程默认(路径和快捷方式自定义,不需要下载jre): 启动后全程 ...
- [No0000155]为什么32位机器最大只能用到4GB内存
在此之前先来了解一些计算机存储单位之间的关系以及计算机系统结构和PC硬件方面的一些知识. 一.计算机存储单位之间的关系 ,最小的存储单位. 个二进制位为一个字节(B),即1B = 8bit,最常用的单 ...
- express链接mysql, 用数据库连接池管理链接
1.在API的开发当中,数据库的处理显得尤为重要,express 工程 链接mysql数据库有很好的模板可以借鉴. 1.1 创建数据库链接 新建一个DB目录,在DB目录下新建文件 db.js 内容如下 ...