什么是content type:django内置的一个组件,这个组件帮忙做连表的操作。(混搭连表)

适用场景:适用于一张表与多张表同时做关联的时候。直接导入就可以使用了。

关联数据库说有的表;让我们可以快速插入数据,并且用反向查找能快速查找到数据。

models.py文件建立表

from django.db import models

# Create your models here.
from django.contrib.contenttypes.fields import GenericForeignKey,GenericRelation
from django.contrib.contenttypes.models import ContentType 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='关联的表名称')
object_id=models.IntegerField(verbose_name='关联的表中的数据行的ID') content_object=GenericForeignKey('content_type','object_id') # 1. 为学位课“Python全栈”添加一个价格策略:一个月 9.9
"""
obj = DegreeCourse.objects.filter(title='Python全栈').first()
# obj.id
cobj = ContentType.objects.filter(model='course').first()
# cobj.id
PricePolicy.objects.create(price='9.9',period='30',content_type_id=cobj.id,object_id=obj.id)
"""

 views.py文件,进行数据插入和查看

from django.shortcuts import render,HttpResponse

# Create your views here.
from app01 import models def test(request):
# 1 为学位课python添加一个价格策略:一个月9.9
# obj1=models.DegreeCourse.objects.filter(title='python').first()
# models.PricePolicy.objects.create(price=9.9,period=30,content_object=obj1)
#
# obj1 = models.DegreeCourse.objects.filter(title='python').first()
# models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
#
# obj1 = models.DegreeCourse.objects.filter(title='python').first()
# models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # # 2 为学位课rest framework添加一个价格策略:一个月9.9
# obj1=models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=9.9,period=30,content_object=obj1)
#
# obj2 = models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=19.9, period=60, content_object=obj2)
#
# obj3 = models.Course.objects.filter(title='rest framework').first()
# models.PricePolicy.objects.create(price=29.9, period=90, content_object=obj3) # 3 根据课程的ID获取课程,并获取该课程的所有价格策略
course=models.Course.objects.filter(id=1).first()
price_policys=course.price_policy_list.all()
print(price_policys)
return HttpResponse('ok')

django之content_type的更多相关文章

  1. Django 组件content_type

    content type: django内置组件,这个组件帮忙做连表操作(混搭连表) 适用场景:适用于一张表与多张表同时做关联的时候.直接导入就可以使用了. 关联数据库所有的表:可以快速插入数据,并且 ...

  2. django model content_type 使用

    一.关于content_type 使用 1.引入模块在models from django.db import models from django.contrib.contenttypes.mode ...

  3. 11.关于django的content_type表

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

  4. Django Rest framework 之 解析器

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  5. django1.7 配置demo教程(环境搭建)

    近期又用到django做个简单项目,1年多没用过了有些手生,按理说没啥问题吧 以下是一个简单的环境搭建demo过程: 前提条件:准备了python2.7已经安装 1.搭建django环境下载 http ...

  6. Django content_type 简介及其应用

    在网上看到 django ORM 有一个 content_type 字段表的应用,这张表不是我们通过建立model类添加的,而是django自动帮我们生成的,具体的作用先简单的举个例子给大家介绍一下. ...

  7. $Django content_type组件 缓存组件

    1  content_type组件(只能方便插入添加) 需求:课程,学位课(不同的课程字段不一样),价格策略 #免费课 class Free_classes (models.Model): id = ...

  8. Django中的content_type表

    models.py from django.db import models from django.contrib.contenttypes.models import ContentType # ...

  9. Django

    一.Django 简介 Django 是一个由 Python 写成的开放源代码的 Web 应用框架.它最初是被开发来用于管理劳伦斯出版集团旗下的一些以新闻内容为主的网站的,即是 CMS(内容管理系统) ...

随机推荐

  1. c++:空构造空析构的益处之一

    项目开发过程中发现一个问题,有段代码,一个动态库的cpp代码里不包含头文件中类的空构造空析构实现,就会出现编译出的动态库依赖项少了很多.而添加后则多了好几个依赖项.下面看例子: ##a.h class ...

  2. CentOS 几种重启方式的区别

    Linux centos重启命令: 1.reboot   普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户 ...

  3. MongoDB之Limit选取Skip跳过Sort排序

    1.Limit选取 我要从Document中取出多少个 只要2条Document db.Wjs.find().limit(2) 2.Skip跳过 我要跳过多少个Document 我要跳过前两个Docu ...

  4. window中磁盘空间不足但是找不到使用空间的文件

    今天看到 电脑的  d盘 空间爆红,空间满了,去找了找没有找到具体是哪个文件占用的空间.一个一个文件的查看属性,都没有找到.文件都不大,几百个g的空间就没了.莫名其妙!!! 自己开启了备份还原,存储的 ...

  5. 发送邮件【文本-html】【图片】【邮件】【附件】

    依赖 <!-- https://mvnrepository.com/artifact/javax.mail/mail --> <dependency> <groupId& ...

  6. 英文谚语:Take that with a grain of salt

    take sth. with a grain of salt 这个习语的字面意思是“和一撮盐一起吃下去”,为什么要与盐一起吃呢? 据说这个习语要追溯到罗马时代,罗马将军庞培曾发现一种解毒剂,必须和着一 ...

  7. Django中反向生成models

    我们在展示django ORM反向生成之前,我们先说一下怎么样正向生成代码. 正向生成,指的是先创建model.py文件,然后通过django内置的编译器,在数据库如mysql中创建出符合model. ...

  8. node.js中module模块的理解

    node.js中使用CommonJS规范实现模块功能,一个单独的文件就是一个单独的模块.通过require方法实现模块间的依赖管理. 通过require加载模块,是同步操作. 加载流程如下: 1.找到 ...

  9. vue 初识组件

    Vue.component("greeting",{ template: `<p>{{ name }}大家好 <button v-on:click="c ...

  10. PHP的网站主要攻击方式有哪些?

    1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) 4.跨网站脚本攻击(Cross Site ...