Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django template using _set.all

I'm working on building a Django app for tracking fiber optic cables. I am running into a problem with what I believe is the relatedmanager, in that in some templates I can get it to do what I want but in others I can't.

A working example of what I'm trying to do:

<ul>
    {% for lanroom in building.lanroom_set.all %}
    <li><a href="/lanrooms/{{ lanroom.id }}/">{{ lanroom.lan_room_name }}</a></li>
    {% endfor %}
</ul>

What this does is gives me the set of LAN rooms that have the building being viewed in detail as a foreign key.

What I am trying to do is have a strand of cable be connected to an adaptor plate connector. So that template has the following:

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

Yet I am not getting any strands that are connected to the adaptor plate connector. Here are the relevant models:

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

How can I get the list of strands that relate to the AdaptorPlateConnector being viewed in detail? Any insight would be much appreciated. I am using generic views, and this template is for a DetailView

like image 778
Michael Avatar asked Mar 08 '13 17:03

Michael


1 Answers

You have two relations from Strand to AdaptorPlateConnector, so you have correctly used related_name to name the reverse relationships. That means you must use one of those names:

{% for strand in adaptorplateconnector.end1_adaptor_plate_connector.all %}

or

{% for strand in adaptorplateconnector.end2_adaptor_plate_connector.all %}

Note that your actual choice of related_name is a bit odd: as demonstrated here, it's the name that you use to refer to Strand from AdaptorPlateConnector.

(Also, not related to this but please don't use _id in the names of ForeignKeys: the underlying database field is an ID, but the field class itself is a reference to the actual related model instance, not the ID.)

like image 182
Daniel Roseman Avatar answered Oct 05 '22 09:10

Daniel Roseman