一.关于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. mission3--dp

    A---母牛的故事 题目大意:第一年有一头母牛,每年年初母牛生小母牛,小母牛第四个年头可以开始生小牛. 问第n年有多少头牛. 题解: (1)列出前几项来找规律(2)第i年牛的数量=第i-1年牛的数量+ ...

  2. CF1175E Minimal Segment Cover

    题目链接 题意 给出n条线段.m次询问,每次询问给出一个区间\([l,r]\)问最少需要多少条线段才能覆盖区间\([l,r]\). 所有坐标\(\le 5\times 10^5\).\(n,m\le ...

  3. stm32环境搭建

    软件下载:https://pan.baidu.com/s/1rat9Vnm 第一部分:软件安装 1.点击安装mdk514.exe,一路next就可以了 2.使用管理员身份打开软件,点击File,进到l ...

  4. 第01组 Beta冲刺(1/5)

    队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11985569.html 作业博客: https://edu.cnblogs.com/campus/fz ...

  5. Oracle逻辑结构学习笔记

    数据库(Database)由若干表空间(Tablespace)组成,表空间(Tablespace)由若干段(Segment)组成,段(Segment)由若干区(Extent)组成,区(Extent)又 ...

  6. c++小学期大作业攻略(零)建议+代码结构(持续更新)

    当前已经做好的exe,数据库是连服务器的,但是头像是存在本地的文件系统里面: https://cloud.tsinghua.edu.cn/d/059ef6b1f9a149ce879b/files/?p ...

  7. 集合类源码(五)Collection之BlockingQueue(LinkedTransferQueue, PriorityBlockingQueue, SynchronousQueue)

    LinkedTransferQueue 功能 全名 public class LinkedTransferQueue<E> extends AbstractQueue<E> i ...

  8. 关于 AutoResetEvent 的介绍的简单示例

    关于 AutoResetEvent 的介绍的简单示例 直接贴代码了: class Program { static void Main(string[] args) { string result = ...

  9. vue cli 3 那些事儿

    配置 eslint 可在 package.json 中的 eslintConfig 中配置,比如允许在代码中使用 console,package.json 中配置如下 ... "eslint ...

  10. redux-thunk形式

    onClick(e) { e.preventDefault(); const { user, pass } = this.refs; this.props.dispatch(login(user.va ...