Reputation: 4316
I would like to look up reviews for items, which users have left in my Django app.
The models are quite simple:
models.py
class Item(models.Model):
name = models.CharField(_('Item'), max_length = 100,)
...
class ItemReview(models.Model):
item = models.ForeignKey(Item)
review = models.CharField(max_length = 140)
...
In my template
I am listing the items
which I pass to the template
from the view function:
items = Item.objects.filter(name = ...)
In my template, I am listing the items like:
{% for item in items %}
<td>{{ item.name }}</td>
{% endfor %}
How can I display the reviews
in the same loop? I have tried FOO_set.all, but it does not seem to work with the filter function.
The problem seems very simple, but I can't get my head around it.
Upvotes: 0
Views: 356
Reputation: 22808
class Item(models.Model):
name = models.CharField(_('Item'), max_length = 100,)
...
def reviews(self):
return ItemReview.object.filter(item=self)
class ItemReview(models.Model):
item = models.ForeignKey(Item)
review = models.CharField(max_length = 140)
{% for item in items %}
<td>
{{ item.name }}
{% for review in item.reviews %}
{{review}}
{% endfor %}
</td>
{% endfor %}
Upvotes: 2