django model content_type 使用
一.关于content_type 使用
1.引入模块在models
from django.db import models
from django.contrib.contenttypes.models import ContentType #使用ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation #正向查询, 反向查询
2. 创建数据库,普通课程, 私人课程, 价格策略
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation class Course(models.Model):
title = models.CharField(max_length=32)
price_policy_list = GenericRelation("PricePolicy") #只是为了反向查询 class DegreeCourse(models.Model):
title = models.CharField(max_length=32)
price_policy_list = GenericRelation("PricePolicy") #只是为了反向查询 class PricePolicy(models.Model):
"""
价格策略
"""
price = models.IntegerField()
period = models.IntegerField() content_type = models.ForeignKey(ContentType, verbose_name="关联的表名称",on_delete=models.CASCADE)
object_id = models.IntegerField(verbose_name="关联表的数据行ID") content_object = GenericForeignKey("content_type", "object_id") #通过找到content_type 找到 关联的表名, object_id 找到行id
最后创建数据库
3.在test.views 增加,反向查询
def test(request):
obj = models.DegreeCourse.objects.filter(title="老人与海").first()#找到对象
models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj)# content_object=obj
obj2 = models.DegreeCourse.objects.filter(title="老人与海").first()
models.PricePolicy.objects.create(price=19.9, period=30, content_object=obj2)
obj3 = models.DegreeCourse.objects.filter(title="老人与海").first()
models.PricePolicy.objects.create(price=29.9, period=30, content_object=obj3) # 反向查询
course = models.DegreeCourse.objects.filter(id=1).first() price_policys = course.price_policy_list.all()
print(price_policys) return HttpResponse("OK")
结果


文章引用 https://www.cnblogs.com/c-x-m/articles/8991616.html#autoid-0-0-0
django model content_type 使用的更多相关文章
- Django Model field reference
===================== Model field reference ===================== .. module:: django.db.models.field ...
- 11.关于django的content_type表
****** Django的contenttype表中存放发的是app名称和模型的对应关系 contentType使用方式 - 导入模块 from django.contrib.contenttype ...
- 【转】Django Model field reference学习总结
Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...
- Django model字段类型清单
转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField ...
- Django:Model的Filter
转自:http://www.douban.com/note/301166150/ django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct 1.多表 ...
- Django model中 双向关联问题,求帮助
Django model中 双向关联问题,求帮助 - 开源中国社区 Django model中 双向关联问题,求帮助
- django 自定用户系统 以及 Django Model 定义语法
http://www.tuicool.com/articles/jMzIr2 django使用自己的用户系统 http://www.jianshu.com/p/c10be59aad7a Django ...
- tornado with MySQL, torndb, django model, SQLAlchemy ==> JSON dumped
现在,我们用torndo做web开发框架,用他内部机制来处理HTTP请求.传说中的非阻塞式服务. 整来整去,可谓之一波三折.可是,无论怎么样,算是被我做成功了. 在tornado服务上,采用三种数据库 ...
- Django model对象接口
Django model查询 # 直接获取表对应字段的值,列表嵌元组形式返回 Entry.objects.values_list('id', 'headline') #<QuerySet [(1 ...
随机推荐
- alarm()函数的使用总结
alarm()函数说明 1.引用头文件:#include <unistd.h>; 2.函数标准式:unsigned int alarm(unsigned int seconds); 3.功 ...
- 第02组 Beta冲刺(3/4)
队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...
- Linux性能优化实战学习笔记:第三十八讲
一.上节回顾 上一节,我们学习了 DNS 性能问题的分析和优化方法.简单回顾一下,DNS 可以提供域名和 IP 地址的映射关系,也是一种常用的全局负载均衡(GSLB)实现方法. 通常,需要暴露到公网的 ...
- WEB前端开发职业学习路线初级完整版
作者 | Jeskson 来源 | 达达前端小酒馆 下面小编专门为广大web前端开发职业者汇总了学习路线初级完整版,其实web前端开发工程师可算是高福利,高薪水的职业了,所以现在学习web前端开发的技 ...
- BeanCreationException: Error creating bean with name 'classPathFileSystemWatcher'之解决办法
错误关键信息: BeanCreationException: Error creating bean with name 'classPathFileSystemWatcher' 错误原因:Idea不 ...
- java包学习之IO
第一 java IO的专题 第二 编码 1.从桌面创建内容为“联”的txt,打开为乱码开始讲起,原因是我们的电脑默认是创建ansi编码的,但是“联”字比较特别,编码会被认为是utf-8的,所以会出现乱 ...
- Java连载9-数据类型&字符编码
一.数据类型注意:(1)计算机最初只支持英文,最先出现的字符编码是:ASII码例如:‘a'对应97,对应01100001(2)编码和解码的时候采用同一套字典/对照表,不会出现乱码.否则会出现乱码.二. ...
- SpringBoot第二篇:配置文件详解一
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10837594.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言 Sprin ...
- 算法(贪心|BF|KMP)
贪心算法 前置知识 const Greedy = num => { //贪心 let arr = [100, 20, 10, 5, 2, 1] let count = 0; for (let i ...
- Python转义序列
正则表达式参考:https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html