Sum in query with F() return None instead zero if value is null

I have this query

sq_edit = UsulanRekomposisiData.objects.filter(file=draft, prk=OuterRef('prk'))
cols = ['edit_jan', 'edit_feb', 'edit_mar', 'edit_apr', 'edit_mei', 'edit_jun', 'edit_jul', 'edit_aug', 'edit_sep', 'edit_okt', 'edit_nov', 'edit_des']
expr = reduce(add, (F(col) for col in cols))
lrpa = monitoring. \
           edit_jan = sq_edit.values('jan'),edit_feb = sq_edit.values('feb'),edit_mar = sq_edit.values('mar'),edit_apr = sq_edit.values('apr'),
           edit_mei = sq_edit.values('mei'),edit_jun = sq_edit.values('jun'),edit_jul = sq_edit.values('jul'),edit_aug = sq_edit.values('aug'),
           edit_sep = sq_edit.values('sep'),edit_okt = sq_edit.values('okt'),edit_nov = sq_edit.values('nov'),edit_des = sq_edit.values('des')
           ).annotate(sum_edit = expr)

I want to annotate the sum of edit in each month but it always return None i guess because one of the month value is null. I want if the value is None the F return 0 or is there any other way to get the sum?

In your reduce you wrote

F(col)  for col in cols

You want

F(col)  for col in cols  if F(col)

(Or use ... if F(col) is not None to preserve zeros, e.g. if you move from sums to averages.)

Back to Top