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 # ...
随机推荐
- php实现多域名共享session会话
php会话机制参考:我的随笔 缘起 网站,通常会有多个服务器,多个子域名,每个节点运行着不同模块.有时为了整体体验,用户用同一个用户名.密码浏览整站,不用重复登录.这时候就需要多服务器共享sessio ...
- css3整理--box-sizing
box-sizing语法: box-sizing : content-box || border-box || inherit 参数取值: content-box:此值为其默认值,其让元素维持W3C的 ...
- mysql中将查询结果进行拼接处理及concat、group_concat的使用
说明: 本文中使用的例子均在下面的数据库表tt2下执行: 一.concat()函数1.功能:将多个字符串连接成一个字符串. 2.语法:concat(str1, str2,...) 返回结果为连接参数产 ...
- 【Linux】解决Android Stadio报错:error in opening zip file
报错: Failed to complete Gradle Execution Cause: error in opening zip file. 原因: 安装gradle失败引起的,往往是上网需要验 ...
- LeetCode 112 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- python字符串魔法
test = 'alEx' print(test.capitalize()) # 首字母大写 其他都变小写 print(test.casefold()) # 变小写 更牛逼 print(test.lo ...
- [No000015D]【李笑来 笔记整理】个人商业模式升级
提前知道地图对你到达目标是至关重要的. 比较女性与男性的成本:女性的成本更高(周期性激素分泌波动),所以不要再这个时候和女朋友较劲,她也是身不由己,当你知道这件事情之后,你就会试着去理解她. 下面这张 ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 Dream hdu6440 Dream 给出一个(流氓)构造法
http://acm.hdu.edu.cn/showproblem.php?pid=6440 题意:让你重新定义任意一对数的乘法和加法结果(输出乘法口诀表和加法口诀表),使得m^p+n^p==(m+n ...
- centos7安装Apache
1.下载安装包wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.37.tar.gz 2.解压tar zxvf httpd-2.4.37.ta ...
- tomcat闪退
tomcat/bin/setclasspath.bat 用记事本打开 在setclasspath.bat的文件里添加两行代码,告诉tomcat,jdk和jre的位置(添加位置在: rem Make s ...