Reputation: 3493
I have two models, Item and Quality. Item has a field named quality which is a FK to Quality.name. The two tables look something like this:
Item
-----
itemId int PK
name varchar
quality varchar FK - Quality.name
Quality
-----
qualityId int PK
name varchar
I'm trying to make a template that lists all Items that have a certain quality. This is my code in question:
{% for Item in items %}
{% if Item.quality == "Common" %}
<div id="name_{{ Item.id }}" class="itemName">{{ Item.name }}</div>
{% endif %}
{% endfor %}
I know that my table contains at least one tuple with quality="Common", but I'm not getting any output.
If I try to condition any other field then it works fine (ex: if Item.name == "exampleName"
).
How do I use an if-statement on a value from a FK?
Upvotes: 0
Views: 27
Reputation: 600026
quality
is the related object, ie an instance of Quality. If you want to compare on the basis of the name, then you have to use that attribute of the instance.
(Also, please use proper cases; instances should not have capital letters.)
{% for item in items %}
{% if item.quality.name == "Common" %}
<div id="name_{{ item.id }}" class="itemName">{{ item.name }}</div>
{% endif %}
{% endfor %}
Upvotes: 3