I want to display all categories with related merchants and related merchant's image. How can I do this?
models.py
class Category(models.Model):
title = models.CharField(max_length = 50)
class Meta:
verbose_name_plural = 'Categories'
class Merchant(models.Model):
category = models.ForeignKey('Category', related_name = 'merchants', blank = True, null = True)
title = models.CharField(max_length = 100)
class StoredFile(models.Model):
merchant = models.OneToOneField(Merchant, related_name="_image", blank = True, null = True)
views.py
categories = Category.objects.select_related()
index.html
{% for category in categories %}
{{ category.title }}
{% for merchant in category.merchants %}
{{ merchant.name }}
{{ merchant.image.url }}
{% endfor %}
{% endfor %}
My code doesn't work.
'RelatedManager' object is not iterable.
I guess related query is wrong.