简体   繁体   中英

Django - get foreign key field name in django template for if statement

in my template i have an if statement to check the type of file, however the file_type field is a foreign key object. how can i get the file_type.type field to use in the if statement?

template:

{% for file in Site.sitefiles_set.all %}
    {% if file.file_type != 'Site Plan' %}

model:

class SiteFileType(models.Model):
    type = models.CharField(max_length=200)

    class Meta:
            verbose_name = "File Type"
            verbose_name_plural = "File Type"    

    def __unicode__(self):
        return self.type

class SiteFiles(models.Model): 
    showroom_config_data = models.ForeignKey(ShowroomConfigData,verbose_name="Showroom")
    major_site_info = models.ForeignKey(MajorSiteInfoData,verbose_name="Major Site")  
    site_image = models.ImageField(blank=True,upload_to=site_files_path)
    site_file = models.FileField(blank=True,upload_to=site_files_path)     
    file_name = models.CharField(max_length=200,verbose_name="File Name (Optional)",blank=True)
    file_type = models.ForeignKey(SiteFileType)

    class Meta:
            verbose_name = "Site Files"
            verbose_name_plural = "Site Files"

    def __unicode__(self):
        return '%s | %s ' % (self.showroom_config_data.location, self.major_site_info.location)

You just need to append type to the variable you have already (which is a SiteFileType object):

{% for file in Site.sitefiles_set.all %}
    {% if file.file_type.type != 'Site Plan' %}
    ...
    {% endif %}
{% endfor %}

只需按照点再上一层即可。

 {% if file.file_type.type != 'Site Plan' %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM