一.关于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. PassGuard密码控件配置

    运行环境 win服务器 系统server2008R2 C# ASP.NET服务器页面 前端部分      1.引用 //JS部分引用 <script type="text/javasc ...

  2. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  3. OsharpNS轻量级.net core快速开发框架简明入门教程-切换数据库(从SqlServer改为MySql)

    OsharpNS轻量级.net core快速开发框架简明入门教程 教程目录 从零开始启动Osharp 1.1. 使用OsharpNS项目模板创建项目 1.2. 配置数据库连接串并启动项目 1.3. O ...

  4. Thread&ThreadPool、Parallel、Async和Await用法总结

    1.线程和线程池Thread&ThreadPool //线程初始化时执行方法可以带一个object参数,为了传入自定义参数,所以执行需单独调用用于传参. Console.WriteLine(& ...

  5. Sitecore个性化 - 基础知识

    许多组织选择Sitecore作为其高级个性化功能的网站平台.Sitecore个性化需要什么以及它能为您的品牌提供什么? 今天, 对于希望提供更好的客户体验的组织来说,个性化不仅仅是一个很好的选择 - ...

  6. 适配器模式(Adapter Pattern)--设计模式

    在生活中,想用苹果充电线给安卓的手机充电时,因为两者的接口不一样,会导致充电口无法进行匹配, 这时候,就需要适配器,将安卓的充电口转化为苹果的接口,这样就可以充电啦.已有的类与新的接口不兼容问题是很普 ...

  7. Why React Is Favored by Front-End Specialists

    In this section, we will discuss some of the features that make React a superior choice for front-en ...

  8. 使用linq对ado.net查询出来dataset集合转换成对象(查询出来的数据结构为一对多)

    public async Task<IEnumerable<QuestionAllInfo>> GetAllQuestionByTypeIdAsync(int id) { st ...

  9. Salesforce LWC学习(一)Salesforce DX配置

    LWC: Create a Salesforce DX Project and Lightning Web Component:https://www.youtube.com/watch?v=p268 ...

  10. Python基础9

    Anacanda软件内更新的方法,而不是每次重装整个软件, 整体更新,省时省力. 但仍要掌握单个包更新的方法.