在模型上过滤时未定义全局名称

时间:2015-11-25 09:32:03

标签: django django-models django-templates django-mptt

我是django mptt的初学者....提前感谢

class Category(MPTTModel):
    title = models.CharField(max_length =120)
    parent = TreeForeignKey('self' , null = True , blank = True , verbose_name='parent category', related_name='categories')

    def get_absolute_url(self):
        return reverse('categories', kwargs={'path': self.get_path()})

    def get_clothes(self): 
        return Product.objects.filter(category__in=self.get_descendants(include_self=True))
    def get_brands(self):
        return  Product.objects.filter(category__in=self.get_descendants(include_self=True) , brand=product__brand)


class Product(models.Model):
    brand = ForeignKey('Brand', verbose_name='brands', related_name='brand' , default='')
    category = TreeForeignKey('Category', verbose_name='categories', related_name='products' , default='')

我有这样的类别 男士>男士服装> T恤衫

我的模板

{% for brand in instance.get_brands %}
{{ brand.title }}
{% endfor %}

我收到错误

global name 'product__brand' is not defined

1 个答案:

答案 0 :(得分:1)

问题出在这个方法中:

def get_brands(self):
        return  Product.objects.filter(category__in=self.get_descendants(include_self=True) , brand=product__brand) #This line

应该是:

return  Product.objects.filter(category__in=self.get_descendants(include_self=True) , brand= 'something') # instead of product__brand