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 # ...
随机推荐
- Unity3D 优化NGUI纹理
原理就是将一张rgba 32的分成两张纹理:一张平台压缩不带alpha和一张为原图1/4大小的压缩图存储alpha信息(用r分量当alpha值),然后修改原材质的Shader传入这两张纹理. 代码如下 ...
- 7.8CSS部分的学习!
<!DOCTYPE html> <html> <head> <title>CSS元素选择器</title> <style type=& ...
- C - 食物链
来源poj1182 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是 ...
- 设计模式学习--Abstarct Factory
What Abstarct Factory:提供一个创建一系列相关或相互依赖的接口,而无需指定他们具体类. Why Abstarct Factory是创建型设计模式的一种,主要在创建对象时解耦,避免对 ...
- 打造研发效率核心竞争力!第40届MPD软件工作坊北京站议题公开
同样是做研发,为什么你的效率总是提不上来?都在寻找创新的技术领域,为何别人总能抢占先机?提升自己的研发竞争力,你都有什么方法? 研发效能已经成为软件企业发展非常核心的竞争力.身处在高速发展的软件研发行 ...
- vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑
导出弹窗的html <template> <Modal v-model="exportModal" width=400 :closable="false ...
- 如何将sql查询出的结果,用符号隔开
晚饭过后,打开QQ圈子,发现QQ群里有人提问了一个问题↓ 数据表中有这样的数据 如何转换为 ,, , 知道写存储过程或者函数可以解决,但是想想能不能用一条sql语句解决...未果... 还是去搜索了下 ...
- PHP一个小函数
// function makeTemp($fileName="index",$ftype=0) { $tempPath="xx/xxxx/{$fileName}.htm ...
- Luogu 3369 / BZOJ 3224 - 普通平衡树 - [无旋Treap]
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3224 https://www.luogu.org/problemnew/show/P3 ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...