我是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
答案 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