Django 1.6:使用 SingleObjectMixin 和 ListView 在模板中显示两个模型

Django 1.6: Displaying two models in template using SingleObjectMixin & ListView

在处理应用程序时,我有很多产品,而一些特定产品只与一个品牌相关。

在我的详细信息视图中,我将逻辑设置为根据在我的品牌模型字段中输入的 slug,在指定模板中显示有关每个品牌的特定信息。我的问题是,当我添加额外的上下文变量来遍历每个产品时,它 returns 我目前在数据库中拥有的所有产品,而不是与基于外键关系的特定品牌相关的产品。

他们有办法解决这个问题吗?谢谢!

Model.py

class Brand(models.Model):
    """
    Information for each brand
    """

    brand_name = models.CharField(max_length=255, blank=True, null=True, unique=True)
    brand_founded = models.IntegerField(max_length=4, null=True)

    brand_origin_city = models.CharField(max_length=255, blank=True, null=True)
    brand_origin_state = models.CharField(max_length=2, blank=True, null=True)

    brand_description = models.TextField(null=True, blank=True)
    brand_product_description = models.TextField(null=True, blank=True)

    #Shows details about specific brand
    slug = models.SlugField(max_length=255, verbose_name=_('Brand Slug'), unique=True,  null=True,  blank=True)

    brand_logo = CloudinaryField('Logo Image', null=True, blank=True)
    brand_feature_image = CloudinaryField('Featured Brand Image', null=True, blank=True)

    brand_website_url = models.URLField(max_length=200, null=True, blank=True)
    brand_email = models.URLField(max_length=200, null=True, blank=True)

    #For the different active states for the brands
    brand_state = models.BooleanField(default=False, verbose_name=_('Brand Available'))
    brand_location_state = models.BooleanField(default=False, verbose_name=_('Location(s) Available'))
    brand_email_state = models.BooleanField(default=False, verbose_name=_('Email Available'))

    # Whether brand is menswear, womenswear or both
    menswear = models.BooleanField(default=False, verbose_name=_('Menswear'))
    womenswear = models.BooleanField(default=False, verbose_name=_('Womenswear'))

    #For Admin Purposes and filtering, to keep track of new and old  in the database by administrative users
    date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
    last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified')) 

class Product(models.Model):
    """
    Products for each brand
    """

    product_name = models.CharField(max_length=255, verbose_name=_('Product Name'), null=True, blank=True)
    product_price = CurrencyField( verbose_name=_('Unit price') )

    # Points to a Cloudinary image
    product_image = CloudinaryField('product image', max_length=255, null=True, blank=True)

    #For Admin Purposes, to keep track of new and old items in the database by administrative users
    date_added = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date added'))
    last_modified = models.DateTimeField(auto_now=True, null=True, blank=True, verbose_name=_('Last modified') )

    # Foreign Key
    brand = models.ForeignKey(Brand, null=True)

View.py

class BrandDetailView(SingleObjectMixin, ListView):

    template_name = 'brand_guides/_brandguide.html'

    def get(self, request, *args, **kwargs):
        self.object = self.get_object(queryset=Brand.objects.all())

        return super(BrandDetailView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(BrandDetailView, self).get_context_data(**kwargs)
        context['brand'] = self.object
        context['product_list'] = Product.objects.filter(brand__product=1)
        return context


    def get_queryset(self,  **kwargs):
    # qs = super(BrandDetailView, self).get_queryset(**kwargs)
        return self.object

url.py

urlpatterns = patterns('',


    #url for brands
    url(r'^(?P<slug>[\w\d-]+)$', BrandDetailView.as_view(), name="brand_view"),
)

brand_guides/_brandguide.html 模板

<div class="section-text align-center mb-70 mb-xs-40">
                            {{ brand.brand_description }}                                  
                        </div>

{% for data in product_list %}
                <li class='col n-4'>
                    <div class='magazine-single-card-container medium'>
                        <div class='magazine-card magazine-product-card'>
                            <div class='magazine-product-img-container'>
                                <div class='hover-container'>

                                    <div class='product-image' style="background-image:url('{% cloudinary_url product.product_image PRODUCT %}');"></div>

                                    <div class='top-right'>
                                        <span class="card-price button-price">${{ data.product_price}}</span>
                                    </div>

                                    <!-- <a class="card-view" href="javascript:void(0)"></a> -->

                                    <label class="card-name">{{ brand.product_name }}</label>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
            {% endfor %}

似乎我需要在 views.py [=11] 中添加 context['product_list'] =] 到产品模型的过滤器查询,因为它已经使用变量 brand 访问与我的品牌模型的外键关系 我只是从我的 get 方法中检索对象的实例并使用它在我的观点范围内。

def get_context_data(self, **kwargs):
        context = super(BrandDetailView, self).get_context_data(**kwargs)
        context['brand'] = self.object
        context['product_list'] = Product.objects.filter(brand=self.object)
        return context