content-type初识

    • 什么是content-type

      • ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中。
      • 当我们的项目做数据迁移后,会有很多django自带的表,其中就有django_content_type表
    • content-type 组件的应用

      • 在model中定义ForeignKey字段,并关联到ContentType表,通常这个字段命名为content-type
      • 在model中定义PositiveIntergerField字段, 用来存储关联表中的主键,通常我们用object_id
      • 在model中定义GenericForeignKey字段,传入上面两个字段的名字
      • 方便反向查询可以定义GenericRelation字段

content-type 实践应用

  需求:

    先提供一个场景,网上商城购物时,会有各种各样的优惠券,比如通用优惠券,满减券,或者是仅限特定品类的优惠券。我们以往的方式是:在数据库中,可以通过外键将 优惠券和不同品类的商品表关联起来:

  

  

from django.db import models

class Electrics(models.Model):
"""
id name
1 日立冰箱
2 三星电视
3 小天鹅洗衣机
"""
name = models.CharField(max_length=32) class Foods(models.Model):
"""
id name
1 面包
2 烤鸭
"""
name = models.CharField(max_length=32) class Clothes(models.Model):
name = models.CharField(max_length=32) class Coupon(models.Model):
"""
id name Electrics Foods Clothes more...
1 通用优惠券 null null null
2 冰箱满减券 2 null null
3 面包狂欢节 null 1 null """
name = models.CharField(max_length=32)
electric_obj = models.ForeignKey(to='Electrics', null=True)
food_obj = models.ForeignKey(to='Foods', null=True)
cloth_obj = models.ForeignKey(to='Clothes', null=True)

初始关系表建立

  将所有的商品都关联到Coupon这张表中,如果是通用优惠券,那么所有的ForeignKey对应字段的值为null,如果仅限某些商品,那么对应商品ForeignKey记录该商品的id,不相关的记录为null。

  

  但是这样做是有问题的:

  1. 实际中商品品类繁多,而且很可能还会持续增加,那么优惠券表中的外键将越来越多,这样我们就要频繁的修改表
  2. 每条记录仅使用其中的一个或某几个外键字段,这样就会造成表空间的浪费。

  

  解决方法:

    通过使用contenttypes 应用中提供的特殊字段GenericForeignKey,我们可以很好的解决这个问题。只需要以下三步:

    • 在model中定义ForeignKey字段,并关联到ContentType表。通常这个字段命名为“content_type”
    • 在model中定义PositiveIntegerField字段,用来存储关联表中的主键。通常这个字段命名为“object_id”
    • 在model中定义GenericForeignKey字段,传入上述两个字段的名字。

    为了更方便查询商品的优惠券,我们还可以在商品类中通过GenericRelation字段定义反向关系。

    

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey class Electrics(models.Model):
name = models.CharField(max_length=32)
coupons = GenericRelation(to='Coupon') # 用于反向查询,不会生成表字段 def __str__(self):
return self.name class Foods(models.Model):
name = models.CharField(max_length=32)
coupons = GenericRelation(to='Coupon') def __str__(self):
return self.name class Clothes(models.Model):
name = models.CharField(max_length=32)
coupons = GenericRelation(to='Coupon') def __str__(self):
return self.name class Coupon(models.Model):
name = models.CharField(max_length=32) content_type = models.ForeignKey(to=ContentType) # step 1
object_id = models.PositiveIntegerField() # step 2
content_object = GenericForeignKey('content_type', 'object_id') # step 3 def __str__(self):
return self.name

models.py

    

 from django.shortcuts import render, HttpResponse
from app01 import models
from django.contrib.contenttypes.models import ContentType def test(request):
if request.method == 'GET':
# ContentType表对象有model_class() 方法,取到对应model
content = ContentType.objects.filter(app_label='app01', model='electrics').first() # 表名小写
cloth_class = content.model_class() # cloth_class 就相当于models.Electrics
res = cloth_class.objects.all()
print(res) # 为三星电视(id=2)创建一条优惠记录
s_tv = models.Electrics.objects.filter(id=2).first()
models.Coupon.objects.create(name='电视优惠券', content_object=s_tv) # 查询优惠券(id=1)绑定了哪些商品
coupon_obj = models.Coupon.objects.filter(id=1).first()
prod = coupon_obj.content_object
print(prod) # 查询三星电视(id=2)的所有优惠券
res = s_tv.coupons.all()
print(res) # 查询obj的所有优惠券:如果没有定义反向查询字段,通过如下方式:
content = ContentType.objects.filter(app_label='app01', model='model_name').first()
res = models.OftenAskedQuestion.objects.filter(content_type=content, object_id=obj.pk).all() return HttpResponse('....')

views.py

  

  总结:

    当一张表和多个表FK关联,并且多个FK中只能选择其中一个或其中n个时,可以利用contenttypes app,只需定义三个字段就搞定!常用的场景:一个商品的多种优惠券,一门课程按照周期的多种价格、一门课程各自的常见问题等等。

content-type 组件的更多相关文章

  1. Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...

  2. Jsoup获取部分页面数据失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.

    用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不符合要求. 请求代码如下: private static ...

  3. SharePoint自动化系列——Add content type to list.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 将创建好的content type(若是跨web application需要事先publish c ...

  4. SharePoint自动化系列——Content Type相关timer jobs一键执行

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 背景: 在SharePoint Central Administration->Monito ...

  5. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  6. the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

    the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header ...

  7. Springs Element 'beans' cannot have character [children], because the type's content type is element-only

    Springs Element 'beans' cannot have character [children], because the type's content type is element ...

  8. springboot 报错 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

    开始 controller 方法写的是 @RequestMapping( value = "/add", method = RequestMethod.POST ) public ...

  9. .NET获取文件的MIME类型(Content Type)

    第一种:这种获取MIME类型(Content Type)的方法需要在.NET 4.5之后才能够支持,但是非常简单. 优点:方便快捷 缺点:只能在.NET 4.5之后使用 public FileResu ...

  10. 万能的ctrl+shift+F(Element 'beans' cannot have character [children], because the type's content type is element-only.错误)

    今天在spring-servlet.xml文件中出现了一个莫名其妙的错误:Element 'beans' cannot have character [children], because the t ...

随机推荐

  1. 给dao层注入jdbcTemplate时的一个强行bug(jdbcDaoSupport不要随便用!用了要记得!)

    记录Dao层一个鱼唇至极的错误 这一天我在使用Spring的进行注解配置项目时, 我的Idea给我抛了一个如下的错误: Exception in thread "main" org ...

  2. Spring MVC深入学习

    一.MVC思想 MVC思想简介:        MVC并不是java所特有的设计思想,也不是Web应用所特有的思想,它是所有面向对象程序设计语言都应该遵守的规范:MVC思想将一个应用部分分成三个基本部 ...

  3. ASP.NET Core 部署IIS及 OFFSET 附近有语法错误解决

    今天自己开发了一个订机票的微信公众号,功能基本已经完成,然后想部署到服务器实际测试下.结果部署上去出现各种问题.先安装asp.net core模块,然后发现数据库并不像在开发时一样,执行ef的命令行语 ...

  4. 重写Ext中的typeOf函数

    重写Ext中的typeOf函数来解决Ext JS中typeOf对字符串对象.元素节点.文本节点.空白文本节点判断并不准确的问题 重写的typeOf函数使用自己实现的TypeOf函数2中的代码 测试代码 ...

  5. [转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

    [转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数 转载自:https://blog.csdn.net/one_money/article/details/56921 ...

  6. java压缩指定目录下的所有文件和文件夹的代码

    将代码过程较好的代码段备份一下,下边资料是关于java压缩指定目录下的所有文件和文件夹的代码,希望对码农有帮助. String sourceDir="E:\test";int pa ...

  7. [20190416]查看shared latch gets的变化.txt

    [20190416]查看shared latch gets的变化.txt 1.环境:SYS@book> @ ver1PORT_STRING                    VERSION  ...

  8. 从0开始的Python学习013编写一个Python脚本

    通过之前的学习我们已经了解了Python的很多基础运用了,现在我们尝试着做一个有使用价值的小脚本. 问题 需求: 我想要一个可以给我备份重要文件的程序. 需求分析: 首先文件是有存储路径,文件的路径和 ...

  9. kubernetes deployment升级和回滚

    a.创建deployment pod kubectl run mynginx --image=docker.io/nginx: --record 准备svc文件 apiVersion: v1 kind ...

  10. 周一02.4变量&垃圾回收机制

    一.变量 1. 什么是变量 量:记录事物的某种状态,即事物典型的特征 变:事物的状态是可以发生变化的 2. 为何要用变量 是为了让计算机能够像人一样记录事物的状态 3. 如何用变量 (先定义后引用) ...