Django模板 - 在模板中获取匹配的外键对象?

时间:2016-05-28 11:12:20

标签: python django

我有一个名为电路的模型,每个电路可能有许多与之相关的文件。

所以在我看来我得到了所有电路和所有文件,然后我试图使用if语句来获取文件以匹配模板中的电路。

我试过了,下面但都回来了,我如何在模板中遍历模型找到匹配?

file.circuit_contract_data
file.circuit_contract_data__id

由于

view.py

@login_required    
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>
        <td>
        {% for file in CircuitFiles %}
            {% if file.circuit_contract_data == item.id %}
            <a href ="{{ MEDIA_URL}}{{ file.circuit_file }}" target="_blank">{{ file.file_name }}</a><br />
            {% endif %} 
        {% endfor %}
        </td>
    </tr>

模型

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

1 个答案:

答案 0 :(得分:4)

循环和比较对象是低效且不必要的,因为你可以query the foreign key relationship backwards这样:

{% 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>
        <td>
        {% for file in item.circuitfiles_set.all %}
            <a href ="{{ file.circuit_file.url }}" target="_blank">{{ file.file_name }}</a><br />
        {% endfor %}
        </td>
    </tr>
{% endfor %}

然后,您可以从上下文中删除CircuitFiles。您还可以使用url的{​​{1}}属性,这样就不需要添加FileField

同样作为Python中的一般约定,只有类名称是驼峰式的 - 所有其他变量都是低级的。遵循此约定可以更容易区分该类的类和对象。