Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django 1.11中的条件表达式_Python_Django_Postgresql_Orm - Fatal编程技术网

Python Django 1.11中的条件表达式

Python Django 1.11中的条件表达式,python,django,postgresql,orm,Python,Django,Postgresql,Orm,我有三种型号:产品、品牌、商店。我想统计给定商店中品牌可用的产品 例如: 阿迪达斯50 Puma 25 现在我有: queryset = Brand.objects .filter(brand_id__in=id_list) .order_by('brand_name') queryset = queryset.annotate(amount_of_products=Count('products')) 但这给了我所有商店的产品数量。 我试过

我有三种型号:
产品、品牌、商店
。我想统计给定
商店中
品牌
可用的
产品

例如:
阿迪达斯50

Puma 25


现在我有:

queryset = Brand.objects
            .filter(brand_id__in=id_list)
            .order_by('brand_name')

queryset =  queryset.annotate(amount_of_products=Count('products'))
但这给了我所有商店的产品数量。

我试过像:

但是对于列表中的每个
品牌
,我得到的
产品数量=1


有没有办法在Django 1.11中实现这个条件表达式?实际上已经解决了这个问题:)

那是不正确的
ForeignKey
使用

queryset = queryset.annotate(
            amount_of_products=Count(
                Case(When(shops__shop_name__in=[shop], then=1))
            ))
queryset = queryset.annotate(
            amount_of_products=Count(
                Case(When(products__shop__shop_name__in=[shop], then=1))
            ))