Django 模板重组
Django template regorup
我想根据成分重新组合。
这是我想要的示例结果
就像下图一样,证书不止一张,所以我把它们排成一行。
Ingredient_name,与该成分相关的所有 stop_name
,
与该成分相关的所有 stop_longitude
等等,在一行 table 中。
现在显示是这样的,可以看到成分名称重复了。
型号
class SupplyChainStops(models.Model):
ingredient = models.ForeignKey(Ingredients, null=True, on_delete=models.CASCADE)
stop_name = models.CharField(max_length=1024, null=True, blank=True)
stop_longitude = models.CharField(max_length=500, null=True, blank=True)
stop_latitude = models.CharField(max_length=500, null=True, blank=True)
is_supplier = models.BooleanField(default=False, blank=True, null=True)
def __str__(self):
return f'{self.stop_name}'
查询
items = SupplyChainStops.objects.all()
模板
{% for item in items %}
<tr class="text-black">
<td>{{ item.ingredient }}</td>
<td>{{ item.stop_name }}
<td>{{ item.stop_longitude }}
<td>{{ item.stop_latitude }}
这是我的数据库结构
这是我想要的输出
您可以为 ForeignKey
字段设置 related_name
。
class SupplyChainStops(models.Model):
ingredient = models.ForeignKey(Ingredients, null=True, on_delete=models.CASCADE, related_name="supply_chain_stops")
可见:
each_row = []
ing_ids = []
sup = SupplyChainStops.objects.all()
for each_sup in sup:
ing_ids.append(each_sup.ingredient.id)
ing_ids = list(set(ing_ids))
sch = []
for each_ing_id in ing_ids:
sch.append(SupplyChainStops.objects.filter(ingredient__id= each_ing_id).last())
for each in sch:
stop_names_list = []
stop_longitude_list = []
stop_latitude_list = []
mi_list = each.ingredient.supply_chain_stops.all()
for each_mi in mi_list:
stop_names_list.append(each_mi.stop_name)
stop_longitude_list.append(each_mi.stop_longitude)
stop_latitude_list.append(each_mi.stop_latitude)
row_list = [each.ingredient, stop_names_list, stop_longitude_list, stop_latitude_list]
each_row.append(row_list)
context = {
"items": each_row
}
在模板中:
{% for item in items %}
<tr class="text-black">
<td>{{ item.0 }}</td>
<td>{{ item.1|join:", " }}</td>
<td>{{ item.2|join:", " }}</td>
<td>{{ item.3|join:", " }}</td>
</tr>
{% endfor %}
我想根据成分重新组合。 这是我想要的示例结果 就像下图一样,证书不止一张,所以我把它们排成一行。
Ingredient_name,与该成分相关的所有 stop_name
,
与该成分相关的所有 stop_longitude
等等,在一行 table 中。
现在显示是这样的,可以看到成分名称重复了。
型号
class SupplyChainStops(models.Model):
ingredient = models.ForeignKey(Ingredients, null=True, on_delete=models.CASCADE)
stop_name = models.CharField(max_length=1024, null=True, blank=True)
stop_longitude = models.CharField(max_length=500, null=True, blank=True)
stop_latitude = models.CharField(max_length=500, null=True, blank=True)
is_supplier = models.BooleanField(default=False, blank=True, null=True)
def __str__(self):
return f'{self.stop_name}'
查询
items = SupplyChainStops.objects.all()
模板
{% for item in items %}
<tr class="text-black">
<td>{{ item.ingredient }}</td>
<td>{{ item.stop_name }}
<td>{{ item.stop_longitude }}
<td>{{ item.stop_latitude }}
这是我的数据库结构
这是我想要的输出
您可以为 ForeignKey
字段设置 related_name
。
class SupplyChainStops(models.Model):
ingredient = models.ForeignKey(Ingredients, null=True, on_delete=models.CASCADE, related_name="supply_chain_stops")
可见:
each_row = []
ing_ids = []
sup = SupplyChainStops.objects.all()
for each_sup in sup:
ing_ids.append(each_sup.ingredient.id)
ing_ids = list(set(ing_ids))
sch = []
for each_ing_id in ing_ids:
sch.append(SupplyChainStops.objects.filter(ingredient__id= each_ing_id).last())
for each in sch:
stop_names_list = []
stop_longitude_list = []
stop_latitude_list = []
mi_list = each.ingredient.supply_chain_stops.all()
for each_mi in mi_list:
stop_names_list.append(each_mi.stop_name)
stop_longitude_list.append(each_mi.stop_longitude)
stop_latitude_list.append(each_mi.stop_latitude)
row_list = [each.ingredient, stop_names_list, stop_longitude_list, stop_latitude_list]
each_row.append(row_list)
context = {
"items": each_row
}
在模板中:
{% for item in items %}
<tr class="text-black">
<td>{{ item.0 }}</td>
<td>{{ item.1|join:", " }}</td>
<td>{{ item.2|join:", " }}</td>
<td>{{ item.3|join:", " }}</td>
</tr>
{% endfor %}