Django中ContentType组件怎么用
发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,这篇文章将为大家详细讲解有关Django中ContentType组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。问题如何在一张表上对多个表进行外键关联fro
千家信息网最后更新 2024年09月30日Django中ContentType组件怎么用
这篇文章将为大家详细讲解有关Django中ContentType组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
问题
如何在一张表上对多个表进行外键关联
from django.db import modelsclass 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 modelsfrom django.contrib.contenttypes.models import ContentTypefrom django.contrib.contenttypes.fields import GenericForeignKey, GenericRelationclass 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测试")
关于"Django中ContentType组件怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
字段
面包
优惠
查询
组件
优惠券
关联
篇文章
苹果
冰箱
对象
折扣
更多
牛奶
电视
香蕉
生成
不错
实用
家用电器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全措施和应急处理预案
什么是下一代网络技术
服务器内存最大多大
计算机网络技术难升本吗
软件开发时期产生的文档
软件开发监狱
数据库解析与管理自学
随州市金銮互联网科技有限公司
大学网络技术需要什么电脑
如何用电脑查服务器
网络安全员工作的主要责任
西安市妮果网络技术
检索效率低的数据库
我的世界服务器9933
泰拉瑞亚公共服务器ip1.4
服务器文件夹过期
网络安全股票的机会
三级数据库2021 百度云
自制小型服务器
工业互联网发展与东土科技
武汉大学 事实数据库培训
深圳华企互联网科技
数据库增加数据步骤
安卓系统软件开发学什么
用来辅助软件开发的工具
手机信号不好无法联接服务器
潍坊红色文化馆软件开发公司
idc软件开发工程师人数
网络安全防范的具体措施
数据库管理员账户密码