【发布时间】: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>
现在我想从 OutdoorInventory 模型中获取 Outdoor_sr_no 并将其传递给 IndoorInventory 查询集,以便在表中 (OD model no) 列旁边的表中也获取outdoor_sr_no 值
【问题讨论】:
标签: django django-models django-views django-templates django-queryset