Django queryset 分组依据并计算相关字段
Django queryset group by and count for related fields
我有这个关系的模型
class Product(models.Model):
code = models.CharField(
_("Code"),
max_length=50,
unique=True
)
name = models.CharField(
_("Name"),
max_length=150
)
class ProductOption(models.Model):
product = models.ForeignKey(Product, verbose_name=_("Product"), on_delete=models.CASCADE, related_name='options')
vtype = models.CharField(_("Type"), max_length=50)
text = models.CharField(_("Text"), max_length=50)
有了这个例子数据
prod1 = Product(code='1', name='Name 1')
ProductOption(product=prod1, vtype='Color', text='Blue')
ProductOption(product=prod1, vtype='Size', text='M')
ProductOption(product=prod1, vtype='Material', text='Cotton')
prod2 = Product(code='2', name='Name 2')
ProductOption(product=prod2, vtype='Color', text='Red')
ProductOption(product=prod2, vtype='Size', text='X')
ProductOption(product=prod2, vtype='Material', text='Cotton')
prod3 = Product(code='3', name='Name 3')
ProductOption(product=prod3, vtype='Color', text='Red')
ProductOption(product=prod3, vtype='Size', text='L')
ProductOption(product=prod3, vtype='Material', text='Cotton')
我如何使用具有此结果的 ORM 从产品构建查询
示例:
Product.objects.filter(name__icontains='some text').values('options__vtype', 'options__text').annotate(count=Count())
[
{'options__vtype': 'Color', 'options__text': 'Blue', 'count': 1},
{'options__vtype': 'Color', 'options__text': 'Red', 'count': 2},
{'options__vtype': 'Size', 'options__text': 'M', 'count': 1},
{'options__vtype': 'Size', 'options__text': 'X', 'count': 1},
{'options__vtype': 'Size', 'options__text': 'L', 'count': 1},
{'options__vtype': 'Material', 'options__text': 'Cotton', 'count': 3},
]
我测试了很多选项,例如使用 Concat 和 Count,但我失败了。
试试这个:
ProductOption.objects.values('vtype', 'text').annotate(count=Count('*'))
我有这个关系的模型
class Product(models.Model):
code = models.CharField(
_("Code"),
max_length=50,
unique=True
)
name = models.CharField(
_("Name"),
max_length=150
)
class ProductOption(models.Model):
product = models.ForeignKey(Product, verbose_name=_("Product"), on_delete=models.CASCADE, related_name='options')
vtype = models.CharField(_("Type"), max_length=50)
text = models.CharField(_("Text"), max_length=50)
有了这个例子数据
prod1 = Product(code='1', name='Name 1')
ProductOption(product=prod1, vtype='Color', text='Blue')
ProductOption(product=prod1, vtype='Size', text='M')
ProductOption(product=prod1, vtype='Material', text='Cotton')
prod2 = Product(code='2', name='Name 2')
ProductOption(product=prod2, vtype='Color', text='Red')
ProductOption(product=prod2, vtype='Size', text='X')
ProductOption(product=prod2, vtype='Material', text='Cotton')
prod3 = Product(code='3', name='Name 3')
ProductOption(product=prod3, vtype='Color', text='Red')
ProductOption(product=prod3, vtype='Size', text='L')
ProductOption(product=prod3, vtype='Material', text='Cotton')
我如何使用具有此结果的 ORM 从产品构建查询
示例:
Product.objects.filter(name__icontains='some text').values('options__vtype', 'options__text').annotate(count=Count())
[
{'options__vtype': 'Color', 'options__text': 'Blue', 'count': 1},
{'options__vtype': 'Color', 'options__text': 'Red', 'count': 2},
{'options__vtype': 'Size', 'options__text': 'M', 'count': 1},
{'options__vtype': 'Size', 'options__text': 'X', 'count': 1},
{'options__vtype': 'Size', 'options__text': 'L', 'count': 1},
{'options__vtype': 'Material', 'options__text': 'Cotton', 'count': 3},
]
我测试了很多选项,例如使用 Concat 和 Count,但我失败了。
试试这个:
ProductOption.objects.values('vtype', 'text').annotate(count=Count('*'))