Aggregate return null values if queryset is none even if using Coalesce with default value

391 Views Asked by At

currently, I am trying to aggregate on queryset. it's working perfectly but the problem is if my queryset is none Coalesce function is not working so default value is set null rather than 0 .

aggregated_data = queryset.aggregate(
                total_trips=Coalesce(Sum("total_trips"), 0, output_field=IntegerField()),
                total_orders=Coalesce(Sum("total_orders"), 0, output_field=IntegerField()),
                total_expense=Coalesce(Sum("total_expense"), 0, output_field=IntegerField()),
                total_payment=Coalesce(Sum("total_payment"), 0, output_field=IntegerField()),
                total_collected_payment=Coalesce(Sum("total_collected_payment"), 0, output_field=IntegerField()),
                total_driver=Coalesce(Sum("total_drivers"), 0, output_field=IntegerField()),
                total_utilized_driver=Coalesce(Sum("total_utilized_drivers"), 0, output_field=IntegerField()),
                total_vehicles=Coalesce(Sum("total_vehicles"), 0, output_field=IntegerField()),
                total_utilized_vehicles=Coalesce(Sum("total_utilized_vehicles"), 0, output_field=IntegerField()),
            )

current output if queryset is none :

{
  "total_trips": null,
  "total_orders": null,
  "total_expense": null,
  "total_payment": null,
  "total_collected_payment": null,
  "total_driver": null,
  "total_utilized_driver": null,
  "total_vehicles": null,
  "total_utilized_vehicles": null
}

expected output :

{
  "total_trips": 0,
  "total_orders": 0,
  "total_expense": 0,
  "total_payment": 0,
  "total_collected_payment": 0,
  "total_driver": 0,
  "total_utilized_driver": 0,
  "total_vehicles": 0,
  "total_utilized_vehicles": 0
}
0

There are 0 best solutions below