一.关于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 使用的更多相关文章

  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. Spring Cloud组件使用/配置小记

    仅使用,无多少技术含量,权记于此以备忘. 微服务架构下的主要组件 服务注册组件:Consul.Etcd等 网关:Zuul.Spring Cloud Gateway等 容错框架:Hystrix 负载均衡 ...

  2. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  3. oracle--10GRAC集群搭建问题OUI-25031

    一,问题描述 安装RAC的过程中在结束 的阶段出现的错误 02,解决方式 这个可能在root.sh 执行的时候报错 由于版本问题: 修改vim /etc/redhat-release 把6.9改为4. ...

  4. mysql修改windows下的data目录

    在windows下,安装完mysql后,建议把数据目录(data目录)移动到非系统目录,避免系统出问题的时候还原. 1.在d盘创建一个mysqldata 2.给新文件增加权限NetworkServic ...

  5. Elasticsearch由浅入深(十)搜索引擎:相关度评分 TF&IDF算法、doc value正排索引、解密query、fetch phrase原理、Bouncing Results问题、基于scoll技术滚动搜索大量数据

    相关度评分 TF&IDF算法 Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequen ...

  6. pgsql_pg的数据类型

    PostgreSQL 提供了丰富的数据类型.用户可以使用 CREATE TYPE 命令在数据库中创建新的数据类型.PostgreSQL 的数据类型被分为四种,分别是基本数据类型.复合数据类型.域和伪类 ...

  7. springboot kafka 消费者

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  8. 如何修改SQL Server 2008 R2数据库的内存

    本篇经验将和大家介绍如何修改SQL Server 2008 R2数据库的内存,希望对大家的工作和学习有所帮助! 工具/原料   SQL Sever 2008 R2数据库已安装 方法/步骤   1 打开 ...

  9. Redis学习之intset整数集合源码分析

    1.整数集合:整数的集合,升序排序,无重复元素 2.整数集合intset是集合键的底层实现之一,当一个集合只包含整数值的元素,并且这个集合的元素数量不多时,redis会使用整数集合作为集合键的底层实现 ...

  10. Docker 创建 Redis 容器

    Docker 创建 Redis 容器 # 配置文件映射: # -v /root/redis/redis.conf:/etc/redis/redis.conf # 数据目录映射: # -v /root/ ...