一.关于content_type 使用

1.引入模块在models

  1. from django.db import models
  2. from django.contrib.contenttypes.models import ContentType #使用ContentType
  3. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation #正向查询, 反向查询

2. 创建数据库,普通课程, 私人课程, 价格策略

  1. from django.db import models
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
  4.  
  5. class Course(models.Model):
  6. title = models.CharField(max_length=32)
  7. price_policy_list = GenericRelation("PricePolicy") #只是为了反向查询
  8.  
  9. class DegreeCourse(models.Model):
  10. title = models.CharField(max_length=32)
  11. price_policy_list = GenericRelation("PricePolicy") #只是为了反向查询
  12.  
  13. class PricePolicy(models.Model):
  14. """
  15. 价格策略
  16. """
  17. price = models.IntegerField()
  18. period = models.IntegerField()
  19.  
  20. content_type = models.ForeignKey(ContentType, verbose_name="关联的表名称",on_delete=models.CASCADE)
  21. object_id = models.IntegerField(verbose_name="关联表的数据行ID")
  22.  
  23. content_object = GenericForeignKey("content_type", "object_id") #通过找到content_type 找到 关联的表名, object_id 找到行id

最后创建数据库

3.在test.views 增加,反向查询

  1. def test(request):
  2. obj = models.DegreeCourse.objects.filter(title="老人与海").first()#找到对象
  3. models.PricePolicy.objects.create(price=9.9, period=30, content_object=obj)# content_object=obj
  4. obj2 = models.DegreeCourse.objects.filter(title="老人与海").first()
  5. models.PricePolicy.objects.create(price=19.9, period=30, content_object=obj2)
  6. obj3 = models.DegreeCourse.objects.filter(title="老人与海").first()
  7. models.PricePolicy.objects.create(price=29.9, period=30, content_object=obj3)
  8.  
  9. # 反向查询
  10. course = models.DegreeCourse.objects.filter(id=1).first()
  11.  
  12. price_policys = course.price_policy_list.all()
  13. print(price_policys)
  14.  
  15. return HttpResponse("OK")

结果

文章引用 https://www.cnblogs.com/c-x-m/articles/8991616.html#autoid-0-0-0

django model content_type 使用的更多相关文章

  1. Django Model field reference

    ===================== Model field reference ===================== .. module:: django.db.models.field ...

  2. 11.关于django的content_type表

    ****** Django的contenttype表中存放发的是app名称和模型的对应关系 contentType使用方式 - 导入模块 from django.contrib.contenttype ...

  3. 【转】Django Model field reference学习总结

    Django Model field reference学习总结(一) 本文档包含所有字段选项(field options)的内部细节和Django已经提供的field types. Field 选项 ...

  4. Django model字段类型清单

    转载:<Django model字段类型清单> Django 通过 models 实现数据库的创建.修改.删除等操作,本文为模型中一般常用的类型的清单,便于查询和使用: AutoField ...

  5. Django:Model的Filter

    转自:http://www.douban.com/note/301166150/   django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct   1.多表 ...

  6. Django model中 双向关联问题,求帮助

    Django model中 双向关联问题,求帮助 - 开源中国社区 Django model中 双向关联问题,求帮助

  7. django 自定用户系统 以及 Django Model 定义语法

    http://www.tuicool.com/articles/jMzIr2 django使用自己的用户系统 http://www.jianshu.com/p/c10be59aad7a Django ...

  8. tornado with MySQL, torndb, django model, SQLAlchemy ==> JSON dumped

    现在,我们用torndo做web开发框架,用他内部机制来处理HTTP请求.传说中的非阻塞式服务. 整来整去,可谓之一波三折.可是,无论怎么样,算是被我做成功了. 在tornado服务上,采用三种数据库 ...

  9. Django model对象接口

    Django model查询 # 直接获取表对应字段的值,列表嵌元组形式返回 Entry.objects.values_list('id', 'headline') #<QuerySet [(1 ...

随机推荐

  1. 洛谷 P4290 [HAOI2008]玩具取名

    传送门 思路 博客半年没更新了,来更新个博文吧 在\(dsr\)聚聚博客的帮助下,我用半个上午和一个中午的时间苟延残喘地完成了这道题 先是读题目读大半天,最后连个样例都看不懂 之后又是想思路,实在想不 ...

  2. [LeetCode] 454. 4Sum II 四数之和之二

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  3. [LeetCode] 95. Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  4. Oracle--DBV命令行工具用法详解及坏块修复

    一,介绍 DBV(DBVERIFY)是Oracle提供的一个命令行工具,它可以对数据文件物理和逻辑两种一致性检查.但是这个工具不会检查索引记录和数据记录的匹配关系,这种检查必须使用analyze va ...

  5. Linux-iostat命令

    查看TPS和吞吐量信息[oracle@oracle01 ~]$ iostatLinux 3.10.0-693.el7.x86_64 (oracle01)     07/31/2019     _x86 ...

  6. Python 腾讯云短信,发送手机验证码

    1.安装包 pip install qcloudsms_py 2.准备好相关参数 腾讯云短信每个月赠送100条短信,申请一个应用,获取appid,然后创建短信签名,然后创建正文模版 3.发送短信 我们 ...

  7. Intellij IDEA使用一 创建javaweb项目并配置tomcat

    一.新建Java web项目 参考:https://blog.csdn.net/chengtengfei352/article/details/79211619 1.点击创建新项目 2. 3.crea ...

  8. Win10 1903 运行安卓模拟器蓝屏解决方案

    由于没有安卓机,想要测试一些东西,所以选择了安卓模拟器,可是一运行模拟器就导致电脑蓝屏,试了 N 次都不行. 于是在网上寻找解决方案,了解到导致蓝屏的原因都是因为虚拟化技术,我的系统是 Windows ...

  9. nacos服务注册与发现及服务配置实现

    Nacos 提供了一组简单易用的特性集,可快速实现动态服务发现.服务配置.服务元数据及流量管理. 更敏捷和容易地构建.交付和管理微服务平台. 关键特性: 服务发现和服务健康监测 动态配置服务 动态 D ...

  10. SpringBoot之RESTful风格

    SpringBoot之RESTful风格 1.RESTful介绍 RESTful是一种软件架构风格,一种时尚! RESTful架构风格规定,数据的元操作,即CRUD(create, read, upd ...