contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中。

models.py文件的表结构写好后,通过makemigrations和migrate两条命令迁移数据后,在数据库中会自动生成一个django_content_type表:

每当我们创建了新的model并执行数据库迁移后,ContentType表中就会自动新增一条记录。比如我在应用api的models.py中创建表class Electrics(models.Model): pass。从数据库查看ContentType表,显示如下:

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

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    electric_id   food_id cloth_id more... # 每增加一张表,关系表的结构就要多加一个字段。
1 通用优惠券   null       null null
2 冰箱满减券   2         null    null
3 面包狂欢节   null       1      null
"""
name = models.CharField(max_length=32)
electric = models.ForeignKey(to='Electrics', null=True)
food = models.ForeignKey(to='Foods', null=True)
cloth = models.ForeignKey(to='Clothes', null=True)

如果是通用优惠券,那么所有的ForeignKey为null,如果仅限某些商品,那么对应商品ForeignKey记录该商品的id,不相关的记录为null。但是这样做是有问题的:实际中商品品类繁多,而且很可能还会持续增加,那么优惠券表中的外键将越来越多,但是每条记录仅使用其中的一个或某几个外键字段。

contenttypes 应用

通过使用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, GenericRelation class Electrics(models.Model):
name = models.CharField(max_length=32)
price = models.IntegerField(default=100)
coupons = GenericRelation(to='Coupon') # 用于反向查询,不会生成表字段 def __str__(self):
return self.name class Foods(models.Model):
name = models.CharField(max_length=32)
price=models.IntegerField(default=100)
coupons = GenericRelation(to='Coupon') def __str__(self):
return self.name class Clothes(models.Model):
name = models.CharField(max_length=32)
price = models.IntegerField(default=100)
coupons = GenericRelation(to='Coupon') def __str__(self):
return self.name class bed(models.Model):
name = models.CharField(max_length=32)
price = models.IntegerField(default=100)
coupons = GenericRelation(to='Coupon') class Coupon(models.Model):
"""
Coupon
id name content_type_id object_id_id
美的满减优惠券 9(电器表electrics) 3
猪蹄买一送一优惠券 10 2
南极被子买200减50优惠券 11 1
"""
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

注意:ContentType只运用于1对多的关系!!!并且多的那张表中有多个ForeignKey字段。

数据化迁移,再给每张表添加数据

衣服表,电器表,床上用品表,美食表

添加完之后,数据迁移

创建记录和查询

from django.shortcuts import render, HttpResponse
from api 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='api', 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='api', model='model_name').first()
res = models.OftenAskedQuestion.objects.filter(content_type=content, object_id=obj.pk).all() return HttpResponse('....')

总结:  当一张表和多个表FK关联,并且多个FK中只能选择其中一个或其中n个时,可以利用contenttypes app,只需定义三个字段就搞定!

创建记录

关系表的结构

用语法给关系表加记录。

添加方式1:

接下来用postmen来发送请求

然后代金券表数据就添加完成了

添加方式2:

通过postmen发送请求结果

查询记录

查询name="电商1代金券"的代金券信息

Django之ContentType详解的更多相关文章

  1. Django入门基础详解

    本次使用django版本2.1.2 安装django 安装最新版本 pip install django 安装指定版本 pip install django==1.10.1 查看本机django版本 ...

  2. HttpWebRequest中的ContentType详解

    1.参考网络资源: http://blog.csdn.net/blueheart20/article/details/45174399  ContentType详解 http://www.tuicoo ...

  3. 第五篇Django URL name 详解

    Django URL name 详解 利用Django开发网站,可以设计出非常优美的url规则,如果url的匹配规则(包含正则表达式)组织得比较好,view的结构就会比较清晰,比较容易维护. Djan ...

  4. Django models Fild详解

    本文参考自:django官方文档models/field 在model中添加字段的格式一般为:  field_name = field_type(**field_options) 一  field o ...

  5. Django model 字段详解

    字段类型选择: AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 ...

  6. Django之model详解

    Django中的页面管理后台 Djano中自带admin后台管理模块,可以通过web页面去管理,有点想php-admin,使用步骤: 在项目中models.py 中创建数据库表 class useri ...

  7. django session入门详解

    概括性的讲: 1.django默认是打开对session的支持的 2.默认情况下session相关的数据会保存在数据库中.浏览器端只保存了session id session 的科普: 1.动态网站中 ...

  8. Django contenttypes 框架详解

    一.什么是Django ContentTypes? Django ContentTypes是由Django框架提供的一个核心功能,它对当前项目中所有基于Django驱动的model提供了更高层次的抽象 ...

  9. Django HttpResponse对象详解

    HttpResponse对象 Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数.那么视图函数在处理完相关的逻辑后,也需要返回一个响 ...

随机推荐

  1. 如何在Java 环境下使用 HTTP 协议收发 MQ 消息

    1. 准备环境在工程 POM 文件添加 HTTP Java 客户端的依赖. <dependency> <groupId>org.eclipse.jetty</groupI ...

  2. WPF 每次只打开一个窗口

    if(downListControl == null || downListControl.IsVisible == false) { downListControl = new DownloadLi ...

  3. Nginx 向客户端输出真实的后端IP地址

    因为涉及到内外网的改造,所以狠多东西现在需要依赖于openresty来做总控实现.然后就碰见了一个比较难办的问题,即在upstream时候,如何获取实际处理请求的server地址.假设有如下upstr ...

  4. [OpenCV] Samples 02: Mat - 图像矩阵

    前言 一.简介 Ref:IplImage, CvMat, Mat 的关系 Mat是opencv2.0推出的处理图像的新的数据结构,现在越来越有趋势取代之前的cvMat和lplImage. 相比之下Ma ...

  5. Java使用选择排序法对数组排序

    编写程序,实现将输入的字符串转换为一维数组,并使用选择排序法对数组进行排序. 思路如下: 点击"生成随机数"按钮,创建Random随机数对象: 使用JTextArea的setTex ...

  6. 7 -- Spring的基本用法 -- 9...容器中Bean的生命周期

    7.9 容器中Bean的生命周期 Spring可以管理singleton作用域的Bean的生命周期,Spring可以精确地知道该Bean何时被创建,何时被初始化完成.容器何时准备销毁该Bean实例. ...

  7. sublime自定义代码段

    打开tools>developer>new snippet 默认代码 <snippet> <content><![CDATA[ Hello, ${1:this ...

  8. Lua协程-测试3

    print("Lua 协程测试3") -- 实现消费者-生产者关系(生产一个就消费一个) count = -- 生产总数 -- 生产者 local newProductorCo = ...

  9. 运行RF测试案例,显示unable to open socket to "localhost:56505" error: [Errno 10061] 错误,且关闭RF卡死的解决办法

    问题描述: 执行WEB ui测试案例后,执行请他的测试案例显示unable to open socket to "localhost:56505" error: [Errno 10 ...

  10. Redis 集群配置

    Redis 集群介绍: (1) 为什么要使用集群:如果数据量很大,单台机器会存在存储空间不够用 .查询速度慢 .负载高等问题,部署集群就是为了解决这些问题(2) Redis 集群架构如下,采用无中心结 ...