【问题标题】:How to merge two Querysets and create new queryset in django如何合并两个查询集并在 django 中创建新的查询集
【发布时间】:2025-12-07 15:25:01
【问题描述】:

我有两个用于空调两部分的模型,即用于显示所有室内机信息的室内机的 IndoorInventory 和用于显示室外机信息的 OutdoorInventory。

class IndoorInventory(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    indoor_brand = models.CharField(max_length=100, null=True, blank=True)
    indoor_model_no = models.CharField(max_length=100, null=True, blank=True)
    indoor_sr_no = models.CharField(max_length=100, null=True, blank=True)
    outdoor_model_no = models.CharField(max_length=100, null=True, blank=True)
class OutdoorInventory(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    product = models.CharField(max_length=50)
    outdoor_brand = models.CharField(max_length=100, null=True, blank=True)
    outdoor_model_no= models.CharField(max_length=100, null=True, blank=True)
    outdoor_sr_no = models.CharField(max_length=100, null=True, blank=True)
    indoor_model_no= models.CharField(max_length=100, null=True, blank=True)

我将 IndoorInventory 的查询集传递给我的 Django 模板并获取那里的所有数据。以下是我的观点

def clientInventory(request):
    context = {}
    client_id = request.GET.get('client_id')

    id_inven = IndoorInventory.objects.filter(client=client_id)
    od_inven = OutdoorInventory.objects.filter(client=client_id)
    
    context['id_inven'] = id_inven
    return render(request, 'client-inventory.html', context)

下面是我的模板

<table id="client-invent-admin" class="table table-striped table-bordered display nowrap">
                                <thead>
                                <tr>
                                    <th class="filterhead">ID</th>
                                    <th class="filterhead">Brand</th>
                                    <th class="filterhead">Product/Type</th>
                                    <th class="filterhead">ID Model No</th>
                                    <th class="filterhead">ID SR No</th>
                                    <th class="filterhead">OD Model No</th>
                                    <th class="filterhead">Actions</th>
                                </tr>
                                <tr>
                                    <th>ID</th>
                                    <th>Brand</th>
                                    <th>Product/Type</th>
                                    <th>ID Model No</th>
                                    <th>ID SR No</th>
                                    <th>OD Model No</th>
                                    <th>Actions</th>
                                </tr>
                                </thead>

                                <tbody>
                                {% for invent in id_inven %}
                                {% if invent.indoor_model_no == '' %}
                                <tr class="bg-danger">
                                    <td>{{ invent.id }}</td>
                                    <td>{{ invent.indoor_brand }}</td>
                                    <td>{{ invent.product }}</td>
                                    <td>{{ invent.indoor_model_no }}</td>
                                    <td>{{ invent.indoor_sr_no }}</td>
                                    <td>{{ invent.outdoor_model_no }}</td>
                                    <td>
                                        <div class="dropdown icon-dropdown">
                                            <a class="btn-icon dropdown-toggle" data-toggle="dropdown" href="#">
                                                <i class="zmdi zmdi-more"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right">
                                                <a class="dropdown-item" href="#"><i class="feather icon-eye"></i> View</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item" href="#"><i class="feather icon-edit"></i> Edit</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item"
                                                   href="/delClient-invent/?invent_id={{invent.id}}&client_id={{client_id}}">
                                                    <i class="feather icon-trash"></i> Delete
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                {% else %}
                                <tr>
                                    <td>{{ invent.id }}</td>
                                    <td>{{ invent.indoor_brand }}</td>
                                    <td>{{ invent.product }}</td>
                                    <td>{{ invent.indoor_model_no }}</td>
                                    <td>{{ invent.indoor_sr_no }}</td>
                                    <td>{{ invent.outdoor_model_no }}</td>
                                    <td>
                                        <div class="dropdown icon-dropdown">
                                            <a class="btn-icon dropdown-toggle" data-toggle="dropdown" href="#">
                                                <i class="zmdi zmdi-more"></i>
                                            </a>
                                            <div class="dropdown-menu dropdown-menu-right">
                                                <a class="dropdown-item" href="#"><i class="feather icon-eye"></i> View</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item" href="#"><i class="feather icon-edit"></i> Edit</a>
                                                <div class="dropdown-divider"></div>

                                                <a class="dropdown-item"
                                                   href="/delClient-invent/?invent_id={{invent.id}}&client_id={{client_id}}">
                                                    <i class="feather icon-trash"></i> Delete
                                                </a>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                {% endif %}
                                {% endfor %}
                                </tbody>

                                <tfoot>
                                <tr>
                                    <th>ID</th>
                                    <th>Brand</th>
                                    <th>Product/Type</th>
                                    <th>ID Model No</th>
                                    <th>ID SR No</th>
                                    <th>OD Model No</th>
                                    <th>Actions</th>
                                </tr>
                                </tfoot>
                            </table>

See table frontend

现在我想从 OutdoorInventory 模型中获取 Outdoor_sr_no 并将其传递给 IndoorInventory 查询集,以便在表中 (OD model no) 列旁边的表中也获取outdoor_sr_no 值

【问题讨论】:

    标签: django django-models django-views django-templates django-queryset


    【解决方案1】:

    您可以使用连接查询集的链方法

    from itertools import chain
    result_list = list(chain(q1, q2, q*))
    

    chain方法返回一个被链接的对象,所以你把它转换成一个列表,然后你就可以得到信息了。

    你也可以使用联合方法(Django Union Method

    qs1.union(qs2)
    

    How can I combine two or more querysets in a Django view?查看更多信息

    【讨论】:

    • 我试过了,但没有得到预期的结果,你能帮我吗,这需要我一整天
    • 当然,你有这个项目的github repo吗?
    • 不,我不使用 github
    • 我应该将所有代码粘贴到问题中吗?
    • 是的,我想这可行。
    【解决方案2】:

    在您的clientInventory 方法中,您可以将od_inven 添加到您的context 字典中。

        context['id_inven'] = id_inven
        context['od_inven'] = od_inven
    

    然后你可以在你的模板中使用它,就像你当前使用id_inven一样。比如:

    ...
    <body>
    {% for foo in od_inven %}
       ...
        <td>{{ foo.id }}</td>
    ...
    

    【讨论】:

    • 我认为这个解决方案不会打印同一张表中的条目