'filtering annotated-aggregated columns with django_filters

Assume I have a Customer Model which is related to an Order Model. I want to list customers and the Sum(or Count) of their orders between a date range. if there was no order in the given range for any customer the annotated column would be 0. if I use django_filters FilterSet it changes the main WHERE clause of the query so the row with no order wouldn't be displayed, but I want something like:

.annotate(
    order_count=Count(
        'customer_orders',
        filter=Q(
            customer_orders__create_time__gte=query_params.get('order_after')
        ) & Q(
            customer_orders__create_time__lte=query_params.get('order_before')
        )
    )
)

is there a neat way to achieve this using django_filters FilterSet?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source