[英]Cannot display foreign key in Django list view
I'm trying to display the foreign key information in my list view for the primary key and it's just not displaying and I cannot for the life of me figure out why.我正在尝试在我的列表视图中显示主键的外键信息,但它只是没有显示,我终生无法弄清楚原因。
Models:楷模:
Primary基本的
class LightHouse(models.Model):
customer = models.ForeignKey('users.CustomUser', on_delete=models.CASCADE, related_name='lighthouse_group')
context_object_name = 'lighthouse'
lighthouseName = models.CharField(max_length=35, default="Not a lighthouse")
Foreign外国的
class Endpoint(models.Model):
lighthouse = models.ForeignKey(LightHouse, on_delete=models.CASCADE, related_name='LHName')
device = models.CharField(max_length=50, unique=True)
Django Template for the PK Django PK 模板
<tbody>
{% for endpoint in lighthouse.endpoint_set.all%}
<ul>
<td>{{ endpoint.device }}</td>
<td>{{ endpoint.endpointIPv4 }}</td>
<td> Online </td>
</ul>
{% endfor %}
</tbody>
What am I missing that it won't display the FK?我错过了什么,它不会显示 FK?
For your question "I cannot for the life of me figure out why."对于您的问题“我一生都无法弄清楚为什么。” the reason is when you try to view a foreign key data you must view it as list of objects for your situation "lighthouse" is a foreign key for "LightHouse" Model so when try to view "lighthouseName" you should display it on this way "{{lighthouse.lighthouseName}}" for single row of data.原因是当您尝试查看外键数据时,您必须根据您的情况将其作为对象列表查看单行数据的“{{lighthouse.lighthouseName}}”。
{% for endpoint in lighthouse.endpoint_set.all%}
<ul>
<td>{{ endpoint.device }}</td>
<td>{{ endpoint.lighthouse.lighthouseName}}</td>
<td> Online </td>
</ul>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.