2017-07-24 50 views
0

我有以下兩種模型。django ListView - 通過加入另一個模型的自定義查詢集

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()) 

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) 
    added_date=models.DateTimeField(blank=False, auto_now=True) 

我有一個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') 

我需要將total stock intotal stock outavailable balance沿着與列表中的視點的每個產品顯示。

現在,我打電話給Product model中定義的函數get_total_stock_in(),get_total_stock_out()get_balance_stock()。但是這會在數據庫上產生大量的重複查詢。

如何將這些數據添加到ListView的查詢集中?

感謝您的任何幫助。

回答

0

讓我在這裏發佈解決方案。

我有我的另一個線程的幫助下解決在django - annotate() - Sum() of a column with filter on another column

這裏是我的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 
相關問題