我有以下模型:
class Item(models.Model):
unique_code = models.CharField(max_length=100)
category_code = models.CharField(max_length=100)
label = models.CharField(max_length=100)
我想得到:
使用的不同category_codes的数量
使用的不同unique_codes数
使用的不同category_code 和unique_code 组合的计数
有什么想法吗?
最佳答案
请求的 Django/SQL 解决方案:
使用的不同category_codes的数量:
category_codes_cnt = Item.objects.values('category_codes').distinct().count()
使用的不同unique_codes的数量:
unique_codes_cnt = Item.objects.values('unique_codes').distinct().count()
使用的不同category_code 和unique_code 的组合的计数:
codes_cnt = Item.objects.values('category_codes', 'unique_codes').distinct().count()
关于python - 计数时合并行 - Django/SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4866013/