我想显示所有类别的相关商家和相关商家的形象。我怎样才能做到这一点?
模型.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)
视图.py
categories = Category.objects.select_related()
索引.html
{% for category in categories %}
{{ category.title }}
{% for merchant in category.merchants %}
{{ merchant.name }}
{{ merchant.image.url }}
{% endfor %}
{% endfor %}
我的代码不起作用。
'RelatedManager' 对象不可迭代。
我猜相关查询是错误的。