You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django单查询获取多对多对象:Entry与Reference关联查询需求

遍历Django中Entry与Reference多对多关联数据的实现方案

嘿,刚好碰到过类似的需求,这就给你拆解清楚怎么实现!

首先先明确你的模型结构:

class Entry(models.Model):
description = models.TextField(blank=True, null=True)
references = models.ManyToManyField(Reference, blank=True)
class Reference(models.Model):
name = models.CharField(max_length=1000, blank=True, null=True)
title = models.CharField(max_length=1000, blank=True, null=True)

1. 基础遍历方式(快速实现但注意性能)

最直接的写法就是遍历所有Entry对象,再逐个访问其关联的Reference集合:

from your_app_name.models import Entry

# 遍历所有Entry
for entry in Entry.objects.all():
    # 获取Entry自身字段值
    print(f"Entry描述: {entry.description or '无描述'}")
    # 遍历关联的Reference
    print("关联的参考资料:")
    for ref in entry.references.all():
        print(f"- 名称: {ref.name or '无名称'}, 标题: {ref.title or '无标题'}")
    print("--- 分割线 ---")

⚠️ 注意:这种写法会触发N+1次数据库查询(1次查所有Entry,每个Entry再查1次关联的Reference),如果Entry数量多的话,性能会很差。

2. 优化版:预加载关联数据(推荐)

用Django的prefetch_related()方法一次性预加载所有关联的Reference数据,把查询次数降到2次(1次查Entry,1次批量查所有关联的Reference):

from your_app_name.models import Entry

# 预加载关联的Reference,避免N+1查询
for entry in Entry.objects.prefetch_related('references').all():
    print(f"Entry描述: {entry.description or '无描述'}")
    print("关联的参考资料:")
    for ref in entry.references.all():
        print(f"- 名称: {ref.name or '无名称'}, 标题: {ref.title or '无标题'}")
    print("--- 分割线 ---")

*为什么用prefetch_related而不是select_related?*因为select_related只适用于一对一/外键的正向关联,而prefetch_related专门处理多对多和反向关联的批量加载。

3. 整理成结构化数据(比如用于API返回)

如果需要把数据整理成字典/JSON格式(比如返回给前端),可以这么写:

from your_app_name.models import Entry
import json

entries_data = []
for entry in Entry.objects.prefetch_related('references').all():
    # 构建单个Entry的字典
    entry_item = {
        "description": entry.description,
        "references": [
            {"name": ref.name, "title": ref.title}
            for ref in entry.references.all()
        ]
    }
    entries_data.append(entry_item)

# 打印格式化后的JSON
print(json.dumps(entries_data, indent=2, ensure_ascii=False))

内容的提问来源于stack exchange,提问作者Nat G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:59:44