模板 django 中带有 prefetch_lated 的 For 循环

发布于 2025-01-13 16:31:11 字数 1611 浏览 3 评论 0原文

我在尝试使用 prefetch_lated 从其他模型访问值时遇到问题

我的模型:

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 %}

单元格中没有内容。我怎样才能显示内容 请帮忙

I got an issue when trying to access values from other models by using prefetch_related

My model:

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()

My views:
....

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})

If I try to print out the content by values, it is ok:

print(get_contract_detail.filter(so_hd=so_hd).values("so_hd","report__noi_dung"))

In my template:

{% for report in contract %}
   {%  for content in report.so_hd.all%}
        <tr>
             <td>{{ forloop.counter }}</td>
             <td>{{content.noi_dung}}</td>
        </tr>
    {% endfor %}
 {% endfor %}

There is no content in cells. How can I show the content
Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2025-01-20 16:31:11

这不起作用的原因是因为使用 .values(…) [Django-doc]。此外,您没有指定 < strong>related_name=… 参数[Django-doc],这意味着您访问reports with .report_set.all()

contract = testimport.objects.filter(
    so_hd__in=get_user, so_hd=so_hd
).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 content in report.report_set.all %}
        <tr>
             <td>{{ forloop.counter }}</td>
             <td>{{ content.noi_dung }}</td>
        </tr>
    {% endfor %}
 {% endfor %}

The reason this does not work is because of the use of .values(…) [Django-doc]. Furthermore you did not specify a related_name=… parameter [Django-doc], so that means that you access the reports with .report_set.all():

contract = testimport.objects.filter(
    so_hd__in=get_user, so_hd=so_hd
).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)

and in the template render with .report_set.all:

{% for report in contract %}
   {% for content in report.report_set.all %}
        <tr>
             <td>{{ forloop.counter }}</td>
             <td>{{ content.noi_dung }}</td>
        </tr>
    {% endfor %}
 {% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文