使用 prefetch_related 在模板 django 中进行循环
For loop in template django with prefetch_related
我在尝试使用 prefetch_related
访问其他模型的值时遇到问题
我的模特:
class testimport(models.Model):
id=models.AutoField(primary_key=True)
so_hd=models.CharField( max_length=50, unique=True)
ten_kh=models.CharField( max_length=500)
tien_dong=models.IntegerField(blank=True, null=True)
created_at=models.DateTimeField(auto_now_add=True)
objects=models.Manager()
def get_absolute_url(self):
return "/chi_tiet_hop_dong/%s/" % self.so_hd
class report(models.Model):
id=models.AutoField(primary_key=True)
so_hd=models.ForeignKey(testimport, on_delete=models.DO_NOTHING, to_field="so_hd")
nhan_vien=models.ForeignKey(Callers, on_delete=models.DO_NOTHING, null=True, blank= True, to_field="admin_id")
noi_dung=models.TextField()
我的看法:
....
get_contract_detail=testimport.objects.filter(so_hd__in=get_user).order_by("id").prefetch_related().values()
contract=get_contract_detail.filter(so_hd=so_hd).all()
return render(request, "caller_template/contract_detail.html", {"contract":contract,"the_next":the_next,"the_prev":the_prev, "so_hd":so_hd,"form":form,"form1":form1})
如果我尝试按值打印出内容,就可以了:
print(get_contract_detail.filter(so_hd=so_hd).values("so_hd","report__noi_dung"))
在我的模板中:
{% for report in contract %}
{% for content in report.so_hd.all%}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{content.noi_dung}}</td>
</tr>
{% endfor %}
{% endfor %}
单元格中没有内容。我怎样才能显示内容
请帮忙
这不起作用的原因是因为使用了 .values(…)
[Django-doc]. Furthermore you did not specify a related_name=…
parameter [Django-doc],所以这意味着您使用 .report_set.all()
:
访问 report
s
contract = testimport.objects.filter(
<strong>so_hd__in=get_user, so_hd=so_hd</strong>
).order_by('id').prefetch_related() # no .values()
context = {
'contract': contract,
'the_next': the_next,
'the_prev': the_prev,
'so_hd': so_hd,
'form': form,
'form1':form1
}
return render(request, 'caller_template/contract_detail.html', context)
并在模板中使用 .report_set.all
渲染:
{% for report in contract %}
{% for <strong>content in report.report_set.all</strong> %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ content.noi_dung }}</td>
</tr>
{% endfor %}
{% endfor %}
我在尝试使用 prefetch_related
访问其他模型的值时遇到问题我的模特:
class testimport(models.Model):
id=models.AutoField(primary_key=True)
so_hd=models.CharField( max_length=50, unique=True)
ten_kh=models.CharField( max_length=500)
tien_dong=models.IntegerField(blank=True, null=True)
created_at=models.DateTimeField(auto_now_add=True)
objects=models.Manager()
def get_absolute_url(self):
return "/chi_tiet_hop_dong/%s/" % self.so_hd
class report(models.Model):
id=models.AutoField(primary_key=True)
so_hd=models.ForeignKey(testimport, on_delete=models.DO_NOTHING, to_field="so_hd")
nhan_vien=models.ForeignKey(Callers, on_delete=models.DO_NOTHING, null=True, blank= True, to_field="admin_id")
noi_dung=models.TextField()
我的看法: ....
get_contract_detail=testimport.objects.filter(so_hd__in=get_user).order_by("id").prefetch_related().values()
contract=get_contract_detail.filter(so_hd=so_hd).all()
return render(request, "caller_template/contract_detail.html", {"contract":contract,"the_next":the_next,"the_prev":the_prev, "so_hd":so_hd,"form":form,"form1":form1})
如果我尝试按值打印出内容,就可以了:
print(get_contract_detail.filter(so_hd=so_hd).values("so_hd","report__noi_dung"))
在我的模板中:
{% for report in contract %}
{% for content in report.so_hd.all%}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{content.noi_dung}}</td>
</tr>
{% endfor %}
{% endfor %}
单元格中没有内容。我怎样才能显示内容 请帮忙
这不起作用的原因是因为使用了 .values(…)
[Django-doc]. Furthermore you did not specify a related_name=…
parameter [Django-doc],所以这意味着您使用 .report_set.all()
:
report
s
contract = testimport.objects.filter(
<strong>so_hd__in=get_user, so_hd=so_hd</strong>
).order_by('id').prefetch_related() # no .values()
context = {
'contract': contract,
'the_next': the_next,
'the_prev': the_prev,
'so_hd': so_hd,
'form': form,
'form1':form1
}
return render(request, 'caller_template/contract_detail.html', context)
并在模板中使用 .report_set.all
渲染:
{% for report in contract %}
{% for <strong>content in report.report_set.all</strong> %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ content.noi_dung }}</td>
</tr>
{% endfor %}
{% endfor %}