Answer the question
In order to leave comments, you need to log in
Django: How to find out in a category the number of brands of products in this category?
Available models:
class Brand(models.Model):
title = models.CharField(max_length=100, verbose_name="Название")
class Category(models.Model):
title = models.CharField(max_length=100, verbose_name="Название")
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.PROTECT, verbose_name='Категория')
brand = models.ForeignKey(Brand, on_delete=models.PROTECT, default=1, verbose_name='Бренд')
title = models.CharField(max_length=100, verbose_name='Название')
def category_list(request):
list_ = Category.objects.annotate(brand_count=Count('product__brand'))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question