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

如何在Django Rest Framework中合并两个模型序列化结果并实现高效查询

合并Django模型查询结果并优化性能

实现方案

方案一:从Product模型出发,关联映射Listing字段

以Product为核心,筛选特定渠道下的产品数据,通过select_related关联关联模型,用annotate将Listing字段映射到Product查询集,最后整理成目标格式:

from django.db.models import F

# 筛选指定渠道(示例为channel_id=1)的产品及对应Listing信息
products_queryset = Product.objects.filter(
    productchannellisting__channel_id=1
).select_related('category', 'productchannellisting').annotate(
    # 把Listing的字段映射到当前查询集
    is_visible=F('productchannellisting__is_visible'),
    price=F('productchannellisting__price')
).values(
    # 指定需要的Product字段、Category字段及映射后的Listing字段
    'name', 'category__id', 'category__name', 'is_visible', 'price'
    # 按需添加Product或Category的其他字段,比如'category__description'
)

# 转换为目标格式
result = []
for item in products_queryset:
    result.append({
        'name': item['name'],
        'category': {
            'id': item['category__id'],
            'name': item['category__name']
            # 按需补充Category的其他字段
        },
        'is_visible': item['is_visible'],
        'price': item['price']
        # 按需补充Product的其他字段
    })

方案二:从ProductChannelListing模型出发,关联查询Product及Category

以渠道Listing为核心,直接查询对应渠道的Listing,再关联查询关联的Product和Category:

# 筛选指定渠道的Listing,同时关联查询对应的Product和Category
listings_queryset = ProductChannelListing.objects.filter(
    channel_id=1
).select_related('product', 'product__category')

# 转换为目标格式
result = []
for listing in listings_queryset:
    product = listing.product
    result.append({
        'name': product.name,
        'category': {
            'id': product.category.id,
            'name': product.category.name
            # 按需补充Category的其他字段
        },
        'is_visible': listing.is_visible,
        'price': listing.price
        # 按需补充Product的其他字段
    })

性能优化说明

两种方案均使用select_related,它会在查询时执行JOIN操作,一次性从数据库拉取所有关联模型的数据,彻底避免了循环遍历中逐个查询关联模型导致的N+1数据库请求问题,保证查询效率。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 07:15:26