django ListView-通过与另一个模型连接来自定义查询集

django ListView-通过与另一个模型连接来自定义查询集,django,listview,django-queryset,Django,Listview,Django Queryset,我有以下两种型号 class Product(models.Model): product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False) manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False) product_type=models.CharField(max_length=2, ch

我有以下两种型号

class Product(models.Model):
    product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False)
    manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False)
    product_type=models.CharField(max_length=2, choices=PRODUCT_TYPE, null=False,blank=False)
    wheel_position=models.CharField(max_length=1, choices=FRONT_BACK, null=False,blank=False)
    opening_stock=models.PositiveIntegerField(default=0)

    class Meta:
        ordering=['product_group','manufacturer']
        unique_together = ('product_group', 'manufacturer','product_type','wheel_position')


    def get_total_stock_in(self):
        sum=Stock.objects.filter(product=self.id, ttype='I').aggregate(Sum('quantity'))

        if sum['quantity__sum'] is not None:
            return sum['quantity__sum']
        else:
            return 0

    def get_total_stock_out(self):
        sum=Stock.objects.filter(product=self.id, ttype='O').aggregate(Sum('quantity'))
        if sum['quantity__sum'] is not None:
            return sum['quantity__sum']
        else:
            return 0

    def get_balance_stock(self):
        return (self.opening_stock+self.get_total_stock_in()
                - self.get_total_stock_out())

我有一个ListView子类来列出所有产品

class ProductList(ListView):
    model=Product
    paginate_by = 20

    def get_queryset(self):
        queryset = super(ProductList, self).get_queryset()
        queryset = queryset.prefetch_related('product_group','product_group__category','manufacturer')
        return queryset.order_by('product_group__category','product_group')
我需要在列表视图中显示每个产品的
总入库
总出库
可用余额

现在,我正在调用
产品模型
中定义的函数
get\u total\u stock\u in()
get\u total\u stock\u out()
get\u balance\u stock()
。但这会在数据库上创建大量重复查询

如何将这些数据添加到ListView的查询集中


谢谢您的帮助。

让我在这里发布解决方案

在另一个线程的帮助下,我得到了我的解决方案

这是我现在的
列表视图

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(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))
        )
        queryset = queryset.annotate(
        balance_stock = ExpressionWrapper(F('opening_stock') + F('stock_in_sum') - F('stock_out_sum'), output_field=DecimalField())
        )

        return queryset
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(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))
        )
        queryset = queryset.annotate(
        balance_stock = ExpressionWrapper(F('opening_stock') + F('stock_in_sum') - F('stock_out_sum'), output_field=DecimalField())
        )

        return queryset