在基于 django class 的视图中使用反向关系

Using reverse relations in django class based views

我有一个django模型如下:

class Category(models.Model):
    category_name = models.CharField(max_length=250, blank=True, null=True)

    class Meta:
        verbose_name = "Category"
        verbose_name_plural = "Categories"

    def __unicode__(self):
        return self.category_name

class Product(models.Model):
    category = models.ForeignKey(Category)
    model_name = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        verbose_name = "Product"
        verbose_name_plural = "Products"

    def __unicode__(self):
        return self.model_name

在我看来,我有一个 ListView 如下:

class CategoryList(ListView):
    model = Category
    template_name = 'categories.html'

而我的categories.html如下:

{% extends "base.html" %}

{% block content %}
    <div>
        <h2>Categories</h2>

        {% for category in object_list %}
        <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
            <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="h{{category.id}}">
                    <h4 class="panel-title">
                        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#{{category.id}}" aria-expanded="true" aria-controls="{{category.id}}">
                          {{category.category_name}}
                        </a>
                    </h4>
                </div>
                <div id="{{category.id}}" class="panel-collapse collapse" role="tabpanel" aria-labelledby="h{{category.id}}">
                    <div class="panel-body">
                        {{category.category_name}} //trying to change here by displaying all model_names!!
                    </div>
                </div>
            </div>
        </div>
        {% endfor %}
{% endblock %}

现在我试图在模板的手风琴内(即在面板主体中)显示一个类别中所有可用产品的列表。如何更改视图以访问这些反向关系?对基于 class 的视图还很陌生?

您可以使用相关字段管理器访问每个类别的相关产品products_set,例如:

{{category.category_name}} //trying to change here by displaying all model_names!!
<ul>
{% for product in category.product_set.all %}
  <li>{{ product.model_name }}</li>
{% endfor %}
</ul>

如果您需要更复杂的过滤,您可能想要在视图中执行此操作或编写自定义模板标记。在视图中执行它看起来像这样:

class CategoryList(ListView):
    model = Category
    template_name = 'categories.html'

    def get_queryset(self):
        qs = super(CategoryList, self).get_queryset()
        for category in qs:
            category.some_filtered_products = category.product_set.filter(...)
        return qs

这将在查看期间解析查询集,并将过滤后的产品放入每个 Category 实例的新内存属性中。