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 # ...
随机推荐
- [原]Jenkins(三)---Jenkins初始配置和插件配置
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/5331 ...
- File not found 错误 nginx
这个错误很常见,很明显找不到文件. 原因是php-fpm找不到SCRIPT_FILENAME里执行的php文件,所以返回给nginx 404 错误. 那么两种情况要么文件真的不存在,要么就是路径错误. ...
- 史上最简洁的UITableView Sections 展示包含NSDicionary 的NSArray
这个最典型的就是电话本,然后根据A-Z分组, 当然很多例子,不过现在发现一个很简洁易懂的: 1. 准备数据,定义一个dictionary来显示所有的内容,这个dictionary对应的value全是数 ...
- 用SpannableString打造绚丽多彩的文本显示效果
extends:http://www.jianshu.com/p/84067ad289d2 引语 TeXtView大家应该都不陌生,文本展示控件嘛! 就用TextView显示普普通通的文本,OK,很简 ...
- 同一个tomcat使用不同http端口配置多个web项目
1.复制 conf/server.xml下的 复制粘贴新的一个Service元素下的所有内容,并修改name为Catalina2,<Service name="Catalina&quo ...
- {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二
python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...
- Codeforces 1038D - Slime - [思维题][DP]
题目链接:http://codeforces.com/problemset/problem/1038/D 题意: 给出 $n$ 个史莱姆,每个史莱姆有一个价值 $a[i]$,一个史莱姆可以吃掉相邻的史 ...
- 牛客多校10 D Rikka with Prefix Sum 不是数据结构
https://www.nowcoder.com/acm/contest/148/D 题意: 1e5个数,1e5个操作,操作分为: 1.区间加. 2.整个数列替换为前缀和. 3.区间查询. 查询数小于 ...
- centos7配置vsftpd
ftp服务器192.168.1.198 1.关闭selinux.关闭防火墙 2.yum -y install vsftpd* 3.开启服务,通过浏览器访问ftp://192.168.1.198,匿名用 ...
- Diagnosing out of memory errors and memory leaks 内存泄露实例 C Java JavaScript 内存泄露
小结: 1. 数据库连接池. JDBC语句和结果对象必须显式地关闭. 2. 电梯到目标楼层后地址是否被释放 When a button is pressed: Get some memory, whi ...