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面试题之一——HTML+CSS(基础部分)
一.HTML + CSS部分 1. 请说明 HTML 文档中 DTD 的意义和作用(酷讯) DTD,文档类型定义,是一种保证 html 文档格式正确的有效方法,在解析网页时,浏览器将使用 DTD 来检 ...
- 【netcore基础】.NET Core使用EPPlus实现MVC API里的Excel导出功能 配置中文表头
EPPlus 用来操作excel非常方便,不用依赖微软的office包,所以推荐使用. 下面是具体步骤和代码 首先用nuget安装 EPPlus.Core 我装的版本是 1.5.4 然后就可以用 Ex ...
- C/C++判断传入的UTC时间是否在今天
在项目中经常会显示一个时间,如果这个时间在今日内就显示为时分秒,否则显示为年月日. 这里先给出一个正确的版本: #include <iostream> #include <time. ...
- xrdp完美实现Windows远程访问Ubuntu 16.04
前言: 在很多场景下,我们需要远程连接到Linux服务器(本文是Ubuntu),传统的连接主要分为两种. 第一种:通过SSH服务(使用xshell等工具)来远程访问,编写终端命令,不过这个是无界面的, ...
- vs2017更新后web项目部分后台代码类没有颜色,也没有自动提示输入功能
vs2017有的版本更新后默认.net framework框架是.net framework4.6.1,将项目的.net framework框架更改为4.6.1,颜色和自动提示出现
- Django之ORM版学员管理系统
班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = models.CharF ...
- HTTP 响应状态码
MDN https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status section 10 of RFC 2616 https://tools.ie ...
- 英语专业出身也要走向python
这两年一直徘徊在学习python和放弃python的道路上不断的徘徊,今年终于没有在蹉跎下去,选择了开始新的自我挑战,零基础开始学习python. 作为一名英语专业毕业的文科生,学习编程还是相对有些困 ...
- python 给字符串加颜色
msg = '\033[41;1m字符串内容\033[0m' print(msg) # \033[41;1m起始位置 改变41数值就是改变其他颜色,.033[0m 结束位置
- CH 4401/Luogu 4168 - 蒲公英 - [分块]
题目链接:传送门 题目链接:https://www.luogu.org/problemnew/show/P4168 题解: 经典的在线求区间众数的问题,由于区间众数不满足区间可加性,所以考虑分块,假设 ...