Django一分钟:django中收集关联对象关联数据的方法

场景:我有一个模型,被其它多个模型关联,我配置了CASCADE级联删除,我想要告知用户删除该实例之后,哪些关联数据将会被一同删除。

假设我们当前有这样一组模型:

python 复制代码
class Warehouse(models.Model):
    """仓库"""
    ...
    
    
class OutboundRecord(models.Model):
    """出库单"""
    warehouse = models.ForeignKey('Warehouse', on_delete=models.CASCADE, verbose_name="仓库")
    ...
    
    
class OutboundList(models.Model):
    """出库单明细"""
    warehouse = models.ForeignKey('Warehouse', on_delete=models.CASCADE, verbose_name="仓库")
    ...


class InboundRecord(models.Model):
    """入库单"""
    warehouse = models.ForeignKey('Warehouse', on_delete=models.CASCADE, verbose_name="仓库")
    ...


class InboundList(models.Model):
    """入库明细"""
    warehouse = models.ForeignKey('Warehouse', on_delete=models.CASCADE, verbose_name="仓库")
    ...

它们的关系如下:

想要查询warehouse所有关联数据的方法很简单,具体代码如下:

python 复制代码
from django.contrib.admin.utils import NestedObjects
from xxx import Warehouse

o = NestedObjects(using='default')
o.collect(Warehouse.object.all())
print(o.nest())

假设我们事先已经插入了一些数据,上述代码会输出一个嵌套数组结构,所有的关系一目了然,嵌套数组的处理也很轻松,根据你的需要进行递归遍历即可。

python 复制代码
[
  <Warehouse: 仓库1>,
  [
    <OutboundRecord: OutboundRecord object (2)>,
    [
      <OutboundList: OutboundList object (2)>,
      <OutboundList: OutboundList object (5)>,
      <OutboundList: OutboundList object (8)>
    ],
    <OutboundRecord: OutboundRecord object (3)>,
    [
      <OutboundList: OutboundList object (3)>,
      <OutboundList: OutboundList object (6)>,
      <OutboundList: OutboundList object (9)>
    ],
    <InboundRecord: InboundRecord object (1)>,
    [
      <InboundList: InboundList object (1)>,
      <InboundList: InboundList object (4)>,
      <InboundList: InboundList object (7)>
    ]
  ],
  <Warehouse: 仓库2>,
  [
    <OutboundRecord: OutboundRecord object (1)>,
    [
      <OutboundList: OutboundList object (1)>,
      <OutboundList: OutboundList object (4)>,
      <OutboundList: OutboundList object (7)>
    ],
    <InboundRecord: InboundRecord object (2)>,
    [
      <InboundList: InboundList object (2)>,
      <InboundList: InboundList object (5)>,
      <InboundList: InboundList object (8)>
    ],
    <InboundRecord: InboundRecord object (3)>,
    [
      <InboundList: InboundList object (3)>,
      <InboundList: InboundList object (6)>,
      <InboundList: InboundList object (9)>
    ]
  ],
  <Warehouse: 仓库3>
]

NestedObjects的collect方法来自父类Collector,不过NestedObjects中多来nest()方法帮我们构建方便的层级结构。

相关推荐
建群新人小猿12 分钟前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神1 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛1 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法2 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter3 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快4 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl5 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5
麦田里的守望者江5 小时前
KMP 中的 expect 和 actual 声明
android·ios·kotlin
Dnelic-5 小时前
解决 Android 单元测试 No tests found for given includes:
android·junit·单元测试·问题记录·自学笔记
vener_5 小时前
LuckySheet协同编辑后端示例(Django+Channel,Websocket通信)
javascript·后端·python·websocket·django·luckysheet