我在使用子代获取父数据时遇到问题。
models.py
class Company_Data(models.Model):
company_name = models.CharField(max_length=254)
company_address = models.CharField(max_length=254)
company_tel_no = models.CharField(max_length=254)
company_fax_no = models.CharField(max_length=254)
class Requirements(models.Model):
req_service = models.CharField(null=False, max_length=254)
req_coverdate_from = models.DateField()
req_coverdate_to = models.DateField()
req_duedates = models.DateField()
company = models.ForeignKey(Company_Data, on_delete=models.CASCADE)
views.py
def business_setup(request):
req = Requirements.objects.filter(req_service = 'Business Setup')
context = {
'req' : req ,
}
return render(request, 'admin_template/content/admin_businesssetup_req.html', context)
模板HTML
{% for requirements in req %}
<table>
<tbody>
<tr>
<th style="width:50%">Company Name</th>
<td>{{ requirements.company_id.company_name }}</td>
</tr>
<tr>
<th> Covered Date From </th>
<td>{{ requirements.req_coverdate_from }}</td>
</tr>
<tr>
<th> Covered Date To </th>
<td>{{ requirements.req_coverdate_to }}</td>
</tr>
<tr>
<th> Due Date </th>
<td>{{ requirements.req_duedates }}</td>
</tr>
</tbody>
</table>
{% endfor %}
我正在尝试requirements.company_id.company_name
显示相关的公司名称,但没有运气。我发现的是查询父项Company_Date
以显示子项Requirements
。但是我想过滤Requirements
表格并显示它的父表格,所以这些表格将不适用于我。
谢谢..
You just have to write "company" instead of "company_id" in your template
<td>{{ requirements.company.company_name }}</td>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句