jmny
jmny

Reputation: 308

How to mix Sum and arithmetic with Django queryset

I 've got this in my code :

   forcasting_order = ProductLine.objects.values('product_name', 'product_id')\                                                                                         
       .filter(delivery_date__date__in=ref_days, order_date__date=Func(F('delivery_date'),function="date"))\                                                            
       .annotate(quantity_to_order=Sum('quantity'))\                                                                                                                    
       .order_by('product_id')                                                                                                                                          


   for x in forcasting_order:                                                                                                                                           
       x['quantity_to_order'] = round(x['quantity_to_order'] / Command.avg_on_x_week)        

Is there a way to divide Sum('quantity') by a constant integer (Command.avg_on_x_week here) inside the query ?

Upvotes: 0

Views: 391

Answers (1)

JoseKilo
JoseKilo

Reputation: 2453

Try something like this:

from django.db.models import Value

.annotate(quantity_to_order=Sum('quantity') / Value(Command.avg_on_x_week))

Upvotes: 1

Related Questions