Django ContentType组件详解
作者:小Pawn爷 时间:2022-04-11 14:19:26
目录
问题
注意
总结
问题
如何在一张表上对多个表进行外键关联
from django.db import models
class Appliance(models.Model):
"""
家用电器表
id name
1 冰箱
2 电视
3 洗衣机
"""
name = models.CharField(max_length=64)
class Food(models.Model):
"""
食物表
id name
1 面包
2 牛奶
"""
name = models.CharField(max_length=32)
class Fruit(models.Model):
"""
水果表
id name
1 苹果
2 香蕉
"""
name = models.CharField(max_length=32)
class Coupon(models.Model):
"""
优惠券表
id name appliance_id food_id fruit_id
1 通用优惠券 null null null
2 冰箱折扣券 1 null null
3 电视折扣券 2 null null
4 苹果满减卷 null null 1
"""
name = models.CharField(max_length=32)
appliance = models.ForeignKey(to="Appliance", null=True, blank=True)
food = models.ForeignKey(to="Food", null=True, blank=True)
fruit = models.ForeignKey(to="Fruit", null=True, blank=True)
注意
1.每增加一张表就需要多增加一个字段,
定义
当一张表要跟多张表进行外键关联的时候,我们可以使用Django提供的ContentType 组件
ContentTypes是Django内置的一个组件,可以追踪项目中所有app和model的对应关系,并记录在ContentType表中
app1/models.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
class Food(models.Model):
"""
id title
1 面包
2 牛奶
"""
title = models.CharField(max_length=32)
# 不会生成coupons字段,只用于反向查询
coupons = GenericRelation(to="Coupon")
class Fruit(models.Model):
"""
id title
1 苹果
2 香蕉
"""
title = models.CharField(max_length=32)
class Coupon(models.Model):
title = models.CharField(max_length=32)
# 第一步:在 model中定义ForeignKey字段,并关联到ContentType表
content_type = models.ForeignKey(to=ContentType, on_delete=None)
# 第二步:定义IntegerField字段,用来存储关联表中的主键
object_id = models.IntegerField()
# 第三步 不会生成字段传入上面两个字段的名字
content_object = GenericForeignKey("content_type", "object_id")
app1\view.py
class DemoView(APIView):
def get(self, request):
# 1.通过ContentType表找表模型
content = ContentType.objects.filter(app_label="app1", model="food").first()
# 获得表model对象 相当于models.app1
model_class = content.model_class()
ret = model_class.objects.all()
print(ret)
# 给面包创建一个优惠券
food_obj = Food.objects.filter(id=1).first()
Coupon.objects.create(title="面包九五折", content_type_id=8, object_id=1)
Coupon.objects.create(title="双十一面包九折促销", content_object=food_obj)
# 正向查询:根据优惠信息查询优惠对象
coupon_obj = Coupon.objects.filter(id=1).first()
content_obj = coupon_obj.content_object
print(content_obj.title)
# 反向查询:查询面包都有哪些优惠券
coupons = food_obj.coupons.all()
print(coupons[0].title)
# 如果没定义反向查询
content = ContentType.objects.filter(app_label="app1", model="food").first()
result = Coupon.objects.filter(content_type=content, object_id=1).all()
print(result[0].name)
return Response("ContentType测试")
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
来源:https://blog.csdn.net/weixin_44689630/article/details/121736537
标签:Django,ContentType,组件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Photoshop设计制作网站流程图解
2007-10-25 12:06:00
![](https://img.aspxhome.com/file/UploadPic/200711/3/2007113203253675s.gif)
JS 显示当前日期+星期(静态)
2007-09-11 13:29:00
FCKeditor技巧之在按钮旁边加文字
2007-10-10 13:17:00
![](https://img.aspxhome.com/file/UploadPic/200710/10/20071010132030615s.png)
Python中的list与tuple集合区别解析
2023-01-13 11:05:23
Oracle 的入门心得 强烈推荐
2009-05-24 19:55:00
Python判断Abundant Number的方法
2023-10-30 02:25:30
python中实现延时回调普通函数示例代码
2023-10-03 02:17:04
python如何发布自已pip项目的方法步骤
2023-01-22 01:17:36
Python cv.Canny()方法参数与使用方法
2023-10-06 07:24:37
![](https://img.aspxhome.com/file/2023/8/62888_0s.png)
Oracle学习笔记(六)
2012-01-05 18:55:27
基于Python实现网页文章转PDF文档
2022-08-23 08:50:17
![](https://img.aspxhome.com/file/2023/2/79422_0s.png)
Python编写生成验证码的脚本的教程
2021-09-13 14:32:22
验证码-挑战你的智慧
2008-09-10 13:08:00
![](https://img.aspxhome.com/file/UploadPic/20089/10/2008910131428605s.jpg)
Python使用Keras OCR实现从图像中删除文本
2022-07-22 20:50:24
![](https://img.aspxhome.com/file/2023/9/80569_0s.jpg)
Python request中文乱码问题解决方案
2023-11-20 16:16:43
我的javascript小扎
2008-08-12 13:04:00
Python 基于win32com客户端实现Excel操作的详细过程
2021-01-18 11:22:47
python 数据提取及拆分的实现代码
2023-11-13 09:13:12
![](https://img.aspxhome.com/file/2023/1/63581_0s.png)
python 获取et和excel的版本号
2022-05-03 09:24:51
PDO::beginTransaction讲解
2023-06-06 00:57:46