【发布时间】:2015-03-23 16:06:17
【问题描述】:
我有模特
class Applicant(models.Model):
job = models.ForeignKey(Job)
location = models.ForeignKey(Location)
type = models.ForeignKey(Type)
type 指的是另一个表:
class Type(models.Model):
""" Model for Applicant Type
Attributes:
type: string
"""
type = models.CharField(max_length=45)
def __str__(self):
""" Override for __str__ method
"""
return self.type
class Meta:
managed = False
db_table = 'jobs_type'
我可以通过简单地编写以下内容来访问模板中的对象类型:
<h3>{{applicant.type}}</h3>
其中applicant 是申请人对象。但是,当我尝试将类型与字符串进行比较时,比较失败:
{% if applicant.type == "Driver" %}
<h3>{{applicant.type}}</h3>
{% else %}
<h3>{{applicant.type}} does not equal "Driver"</h3>
{% endif %}
印刷:
Driver does not equal "Driver"
有没有更好的方法来比较 Django 模板中的对象字段?
数据库的jobs_type表中的“Type”字段是一个varChar。
【问题讨论】:
标签: python django django-templates django-views