Django combine search filter and annotation does not work

i am try to filter by datetime range but get wrong annotation results.

def is_valid_queryparam(param):
return param != '' and param is not None

def my_func(request, tenant):
    products = Product.objects.filter(tenant=tenant).annotate(
     s_product=Sum('order_items__quantity'), vlera=Sum('order_items__price'))
    date_min = request.GET.get('date_min')
    date_max = request.GET.get('date_max')

    if is_valid_queryparam(date_min):
        products = products.filter(Q(created__gte=date_min))

    if is_valid_queryparam(date_max ):
        products = products.filter(Q(created__lte=date_max))

the filter date range works for objects products, but not for the order_items__quantity and order_items__price, the result are incorrect

Back to Top