0

I need to display the values of the categories field from the class Analiza from the models below in a template.

class Category(models.Model):
    name = models.CharField(max_length=60)
    def __unicode__(self):
        return self.name

class Analiza(models.Model):
    ...
    categories = models.ManyToManyField(Category, blank = True, null = True, verbose_name = "Категорија")
    ...

How do I do that? I've been reading documentation, but no reference to a situation of this kind (ManyToMany of ForeignKey).

Thanks in advance.

4

1 回答 1

1

给定一个类 Analiza 的实例,它将有一个categories可以在模板中引用的多对多字段管理器属性:

<ul>
{% for category in obj.categories.all %}
    <li>{{ category }}</li>
{% endfor %}
</ul>

或者其他什么——关键是它将是一个可迭代的返回类别的实例。

于 2013-05-16T22:52:35.933 回答