django - annotate() - 对另一列进行过滤的列的 Sum()
django - annotate() - Sum() of a column with filter on another column
我有以下两个型号。
class Product(models.Model):
product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False)
manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False)
opening_stock=models.PositiveIntegerField(default=0)
class Meta:
unique_together = ('product_group', 'manufacturer')
和
TRANSACTION_TYPE=(('I','Stock In'),('O','Stock Out'))
class Stock(models.Model):
product=models.ForeignKey('product.Product', blank=False,null=False)
date=models.DateField(blank=False, null=False,)
quantity=models.PositiveIntegerField(blank=False, null=False)
ttype=models.CharField(max_length=1,verbose_name="Transaction type",choices=TRANSACTION_TYPE, blank=False)
我需要列出所有带有 stock_in_sum=Sum(of all stock ins)
、 stock_out_sum=Sum(of all stock outs)
和 blance_stock=opening_stock+stock_in_sum - stock_out_sum
的产品
这是我到目前为止所取得的成就。
class ProductList(ListView):
model=Product
def get_queryset(self):
queryset = super(ProductList, self).get_queryset()
queryset = queryset.prefetch_related('product_group','product_group__category','manufacturer')
queryset = queryset.annotate(stock_in_sum = Sum('stock__quantity'))
queryset = queryset.annotate(stock_out_sum = Sum('stock__quantity'))
我需要
stock_in_sum
作为 sum(quantity) where ttype='I'
stock_out_sum
作为 sum(quantity) where ttype='O'
blance_stock
作为 product.opening_stock + stock_in_sum - stock_out_sum
以及每个产品对象。
如何实现?
谢谢。
你可以使用 conditional aggregation
queryset = queryset.annotate(
stock_in_sum = Sum(Case(When(stock__ttype='I', then=F('stock__quantity')), output_field=DecimalField(), default=0)),
stock_out_sum = Sum(Case(When(stock__ttype='O', then=F('stock__quantity')), output_field=DecimalField(), default=0)))
)
求和,然后用F() expression
计算余额
queryset = queryset.annotate(balance_stock=F('opening_stock') + F('stock_in_sum') - F('stock_out_sum'))
您还可以链接不同的操作而不是多重赋值:
queryset = queryset.prefetch_related(...).annotate(...).annotate(...)
我有以下两个型号。
class Product(models.Model):
product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False)
manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False)
opening_stock=models.PositiveIntegerField(default=0)
class Meta:
unique_together = ('product_group', 'manufacturer')
和
TRANSACTION_TYPE=(('I','Stock In'),('O','Stock Out'))
class Stock(models.Model):
product=models.ForeignKey('product.Product', blank=False,null=False)
date=models.DateField(blank=False, null=False,)
quantity=models.PositiveIntegerField(blank=False, null=False)
ttype=models.CharField(max_length=1,verbose_name="Transaction type",choices=TRANSACTION_TYPE, blank=False)
我需要列出所有带有 stock_in_sum=Sum(of all stock ins)
、 stock_out_sum=Sum(of all stock outs)
和 blance_stock=opening_stock+stock_in_sum - stock_out_sum
这是我到目前为止所取得的成就。
class ProductList(ListView):
model=Product
def get_queryset(self):
queryset = super(ProductList, self).get_queryset()
queryset = queryset.prefetch_related('product_group','product_group__category','manufacturer')
queryset = queryset.annotate(stock_in_sum = Sum('stock__quantity'))
queryset = queryset.annotate(stock_out_sum = Sum('stock__quantity'))
我需要
stock_in_sum
作为sum(quantity) where ttype='I'
stock_out_sum
作为sum(quantity) where ttype='O'
blance_stock
作为product.opening_stock + stock_in_sum - stock_out_sum
以及每个产品对象。
如何实现?
谢谢。
你可以使用 conditional aggregation
queryset = queryset.annotate(
stock_in_sum = Sum(Case(When(stock__ttype='I', then=F('stock__quantity')), output_field=DecimalField(), default=0)),
stock_out_sum = Sum(Case(When(stock__ttype='O', then=F('stock__quantity')), output_field=DecimalField(), default=0)))
)
求和,然后用F() expression
计算余额queryset = queryset.annotate(balance_stock=F('opening_stock') + F('stock_in_sum') - F('stock_out_sum'))
您还可以链接不同的操作而不是多重赋值:
queryset = queryset.prefetch_related(...).annotate(...).annotate(...)