温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Django的ContentType怎么使用

发布时间:2021-12-27 10:05:57 来源:亿速云 阅读:98 作者:iii 栏目:大数据

本篇内容主要讲解“Django的ContentType怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Django的ContentType怎么使用”吧!

contenttypes 是Django内置的一个应用,可以追踪项目中所有app和model的对应关系,并记录在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字段。  

到此,相信大家对“Django的ContentType怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI