我在 django 应用程序中有一个模型,具有以下结构:
class items(models.Model):
name = models.CharField(max_length=50)
location = models.CharField(max_length=3)
Run Code Online (Sandbox Code Playgroud)
我想为每个名称/项目的每个位置的计数创建一个数据透视表,我设法按照以下方式进行:
queryset_res = items.objects.values('name')\
.annotate(NYC=Sum(Case(When(location='NYC', then=1),default=Value('0'),output_field=IntegerField())))\
.annotate(LND=Sum(Case(When(location='LND', then=1),default=Value('0'),output_field=IntegerField())))\
.annotate(ASM=Sum(Case(When(location='ASM', then=1),default=Value('0'),output_field=IntegerField())))\
.annotate(Total=Count('location'))\
.values('name', 'NYC', 'LSA','Total')\
.order_by('-Total')
Run Code Online (Sandbox Code Playgroud)
这给了我每个名字在每个位置出现的次数,这一切都很好。
我的问题是如何使位置动态化,因此如果添加了新位置,我就不会再回来更改代码!来自列表或模型数据本身
非常感谢AB