AlexW AlexW - 2 years ago 160
Python Question

Django template - get matching foreign key objects in a template?

I have a model named circuits and each circuit may have many files associated to it.

so in my view i am getting all the circuits and all the files, then i am trying to get the files to match the circuits in my template using an if statement.

i have tried, the below but all come back empty, how do i traverse models in a template to find a match?



def showroom_detail(request, showroom_id):
modelShowroom = get_object_or_404(ShowroomConfigData, pk=showroom_id)
modelCircuits = CircuitInfoData.objects.filter(showroom_config_data=showroom_id)
modelCircuitFiles = CircuitFiles.objects.filter(circuit_contract_data__showroom_config_data=showroom_id)
modelSitePhotos = SitePhotos.objects.filter(showroom_config_data=showroom_id)
modelSiteFiles = SiteFiles.objects.filter(showroom_config_data=showroom_id)

return render(request, 'service/showroom_detail.html', {
'Showroom': modelShowroom,
'Circuits': modelCircuits,
'CircuitFiles': modelCircuitFiles,
'SitePhotos': modelSitePhotos,
'SiteFiles': modelSiteFiles,


{% for item in Circuits %}
<tr class="{% cycle 'tr-1' 'tr-2' %}">
<td>{{ item.provider }}</td>
<td>{{ item.service_type }}</td>
<td>{{ item.circuit_speed }}</td>
{% for file in CircuitFiles %}
{% if file.circuit_contract_data == %}
<a href ="{{ MEDIA_URL}}{{ file.circuit_file }}" target="_blank">{{ file.file_name }}</a><br />
{% endif %}
{% endfor %}


class CircuitInfoData(models.Model):
showroom_config_data = models.ForeignKey(ShowroomConfigData,verbose_name="Install Showroom",blank=True)
major_site_info = models.ForeignKey(MajorSiteInfoData,verbose_name="Install Site",blank=True)
service_type = models.ForeignKey(CircuitTypeInfoData)
circuit_speed = models.IntegerField(blank=True)
circuit_bearer = models.IntegerField(blank=True)
provider = models.CharField(max_length=200)
ref_no = models.CharField(max_length=200,verbose_name="Reference No")
install_location = models.CharField(max_length=200)
install_date = models.DateField()
cost_per_month = models.DecimalField(decimal_places=2,max_digits=8)
contract_length = models.IntegerField(verbose_name="Contact length in years")
notes = models.TextField(blank=True)
service_service_contacts = models.ForeignKey(ServiceContacts)

class Meta:
verbose_name = "Circuit Data"
verbose_name_plural = "Circuit Data"
ordering = ('showroom_config_data__location',)

def __unicode__(self):
return '%s | %s | %s | %s' % (self.showroom_config_data.location, self.provider, self.service_type, self.ref_no)

class CircuitFiles(models.Model):
circuit_contract_data = models.ForeignKey(CircuitInfoData,verbose_name="Showroom",blank=True)
circuit_file = models.FileField(blank=True,upload_to=service_upload_path)
file_name = models.CharField(max_length=200,verbose_name="File Name")

class Meta:
verbose_name = "Circuit Files"
verbose_name_plural = "Circuit Files"
ordering = ('circuit_contract_data',)

def __unicode__(self):
return self.file_name

Answer Source

Looping through and comparing the objects is inefficient and unnecessary because you can query the foreign key relationship backwards like so:

{% for item in Circuits %}
    <tr class="{% cycle 'tr-1' 'tr-2' %}">  
        <td>{{ item.provider }}</td>
        <td>{{ item.service_type }}</td>
        <td>{{ item.circuit_speed }}</td>
        {% for file in item.circuitfiles_set.all %}
            <a href ="{{ file.circuit_file.url }}" target="_blank">{{ file.file_name }}</a><br />
        {% endfor %}
{% endfor %}

You can then drop the CircuitFiles from your context. You can also use the url property of a FileField so that you don't need to prepend MEDIA_ROOT.

Also as general convention in Python, only class names are camel cased - all other variables are lower cased. Following this convention makes it easier to distinguish between a class and an object of that class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download