Mic*_*ael 3 django django-templates django-models
我正在开发用于跟踪光缆的Django应用程序.我遇到了我认为是相关管理员的问题,因为在某些模板中,我可以让它做我想做的事,但在其他模板中,我做不到.
我正在尝试做的一个工作示例:
<ul>
{% for lanroom in building.lanroom_set.all %}
<li><a href="/lanrooms/{{ lanroom.id }}/">{{ lanroom.lan_room_name }}</a></li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
这样做给了我一组LAN房间,这些房间的建筑物被详细查看为外键.
我想要做的是将一股电缆连接到适配器板连接器.所以该模板具有以下内容:
<li>Date Added: {{ adaptorplateconnector.date_added }}</li>
<li>Connector Type in {{ adaptorplateconnector }}:</li>
<ul>
<li><a href="/connectortypes/{{ adaptorplateconnector.connector_type_id.id }}/">{{ adaptorplateconnector.connector_type_id.type }}</a></li>
</ul>
<li>Strand connected to {{ adaptorplateconnector }}:
<ul>
{% for strand in adaptorplateconnector.strand_set.all %}
<ali><a href="/strands/{{ strand.id }}/">{{ strand }}</a></li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)
然而,我没有得到任何连接到转接板连接器的绞线.以下是相关型号:
class AdaptorPlateConnector(models.Model):
adaptor_plate_id = models.ForeignKey('AdaptorPlate')
connector_type_id = models.ForeignKey('ConnectorType')
strand_position = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.strand_position
class Strand(models.Model):
cable_id = models.ForeignKey('Cable')
end1_plate_connector_id = models.ForeignKey('AdaptorPlateConnector', related_name= 'end1_adaptor_plate_connector')
end2_plate_connector_id = models.ForeignKey('AdaptorPlateConnector', related_name= 'end2_adaptor_plate_connector')
in_use = models.CharField(max_length=200)
desc = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return "End1: " + self.end1_plate_connector_id.__unicode__() + ", End2: " + self.end2_plate_connector_id.__unicode__() + ", Cable: " + self.cable_id.__unicode__()
Run Code Online (Sandbox Code Playgroud)
如何获取与详细查看的AdaptorPlateConnector相关的链的列表?任何见解都会非常感激.我正在使用通用视图,此模板用于DetailView
你有两个关系从Strand到AdaptorPlateConnector,所以你必须正确使用related_name命名的反向关系.这意味着您必须使用以下名称之一:
{% for strand in adaptorplateconnector.end1_adaptor_plate_connector.all %}
Run Code Online (Sandbox Code Playgroud)
要么
{% for strand in adaptorplateconnector.end2_adaptor_plate_connector.all %}
Run Code Online (Sandbox Code Playgroud)
需要注意的是related_name的实际选择是有点奇怪:作为展示的一样,这是您用来参考的名字Strand从AdaptorPlateConnector.
(另外,与此无关,但请不要_id在以下名称中使用ForeignKeys:底层数据库字段是一个ID,但字段类本身是对实际相关模型实例的引用,而不是ID.)
| 归档时间: |
|
| 查看次数: |
8582 次 |
| 最近记录: |