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

Django中如何对带有ManyToManyField的表执行内连接查询?

实现方案

你定义的是多对多关联关系,Django ORM会自动生成中间表存储两张表的关联关系,要实现内连接并获取全量字段可以用以下三种方案,按需选择:

方案1:ORM预加载(最常用,推荐)

使用prefetch_related预加载关联数据,自动处理关联逻辑,可直接访问完整模型对象的所有字段,同时避免N+1查询问题:

# 预加载关联的Price对象
edu_queryset = Education.objects.prefetch_related("price").all()

# 访问字段示例
for edu in edu_queryset:
    # Education表字段
    print(f"课程标题:{edu.title},课程内容:{edu.content}")
    # 关联的Price表所有字段
    for price_item in edu.price.all():
        print(f"价格:{price_item.cost},创建时间:{price_item.created_at}")

方案2:扁平化查询结果

如果需要返回两个表字段合并的扁平化字典结构,可使用values方法跨表取字段,ORM自动执行内连接:

# filter条件过滤掉没有关联价格的课程,严格符合内连接语义
flat_result = Education.objects.values(
    "title", "content", "price__cost", "price__created_at"
).filter(price__isnull=False)

# 直接通过key访问所有字段
for item in flat_result:
    print(item["title"], item["content"], item["price__cost"], item["price__created_at"])

方案3:原生SQL查询

如果有特殊性能优化需求,也可以手写原生SQL执行内连接:

from django.db import connection

def get_edu_price_join_data():
    with connection.cursor() as cursor:
        # 注意将SQL中的yourapp替换为你的Django应用名
        cursor.execute("""
            SELECT e.title, e.content, p.cost, p.created_at
            FROM yourapp_education e
            INNER JOIN yourapp_education_price ep ON e.id = ep.education_id
            INNER JOIN yourapp_price p ON ep.price_id = p.id
        """)
        # 自动映射字段名,返回字典列表
        field_names = [desc[0] for desc in cursor.description]
        return [dict(zip(field_names, row)) for row in cursor.fetchall()]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 11:36:00