英文:
How to convert Django Queryset into a list in template?
问题
以下是已翻译的代码部分:
class Building(models.Model):
Building_code = models.CharField(primary_key=True, max_length=255)
Building_name = models.CharField(max_length=255)
@property
def BuildingFullName(self):
fullname = '{} | {}'.format(self.Building_code, self.Building_name)
return fullname
def __str__(self):
return self.Building_code
<tbody>
{% for hira in hiras %}
<tr>
<td>{{ hira.Activity }}</td>
<td>{{ hira.Hazard.Hazard }}</td>
<td>{{ hira.PBS.Code }}</td>
{% with allbuilding=hira.Building.all %}
<td>{{ allbuilding }}</td>
{% endwith %}
<td>{{ hira.Building.Function_Mode }}</td>
{% endfor %}
</tbody>
希望这可以帮助你解决问题。如果你需要进一步的帮助,请告诉我。
英文:
Here is my model :
class Building(models.Model):
Building_code = models.CharField(primary_key=True, max_length=255)
Building_name = models.CharField(max_length=255)
@property
def BuildingFullName(self):
fullname = '{} | {}'.format(self.Building_code, self.Building_name)
return fullname
def __str__(self):
return self.Building_code
What i do in my template :
<tbody>
{% for hira in hiras %}
<tr>
<td>{{ hira.Activity }}</td>
<td>{{ hira.Hazard.Hazard }}</td>
<td>{{ hira.PBS.Code }}</td>
{% with allbuilding=hira.Building.all %}
<td>{{ allbuilding }}</td>
{% endwith %}
<td>{{ hira.Building.Function_Mode }}</td>
{% endfor %}
</tbody>
This is what I am getting :
But I would like the building list to be displayed without this "<Queryset". For the first row, it would be "13, 14".
答案1
得分: 1
你可以使用 join
:
{% with allbuilding=hira.Building.all %}
<td>{{ allbuilding|join:", " }}</td>
{% endwith %}
英文:
You can use join
:
{% with allbuilding=hira.Building.all %}
<td>{{ allbuilding|join:", " }}</td>
{% endwith %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论