【发布时间】:2022-11-10 23:13:04
【问题描述】:
嗨,我是 django 编码的新手 我在不同的 Django 应用程序中有 3 个表(模型),我尝试在 htmpl 页面中制作一个简单的报告,因此需要根据外键检索每个项目的特定数据。
通过下面的代码,我可以访问 Html 页面中的机器存储表 做一个循环,但我想从其他表中获取数据并过滤它们,
{% for m in machines %}
<h2>{{m.title}}</h2>
<h3>{{}}</h3>.
//i** need to access the ordertitle field in tb_order base on machine id. ??**
<p>{{ }}</p>.
// **access ordertitle in tb_order base on status field ??**
{%end for %}`
这是一个view.py
def allmachinesLOGO(request):
machines=Machine.objects.all()
context ={'machines':machines}
return render(request,'pline/machineslogos.html',context)
楷模 :
class tb_order(models.Model):
ordertitle= models.CharField(max_length=200)
customer=models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
mould=models.ForeignKey(Mould, null=True, on_delete=models.SET_NULL, blank=True)
status=models.CharField(choices=status_choices, default="0", max_length=20)
accept=models.BooleanField
machine=models.ForeignKey(Machine,null=True, on_delete=models.SET_NULL, blank=True)
id =models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False, unique=True)`
class Machine(models.Model):
title = models.CharField(max_length=200)
machine_model = models.CharField(max_length=200, null=True, blank=True)
description = models.CharField(max_length=600, null=True, blank=True)
featured_image=models.ImageField(default="defaultmachine.webp", blank=True, null=True)
id=models.UUIDField(default=uuid.uuid4, unique=True,primary_key=True,editable=False)`
建议解决方案根据状态字段访问 tb_order 中的 ordertitle ?? // i need to access the ordertitle field in tb_order base on machine id.
【问题讨论】:
标签: django