0

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. \
           annotate(
           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?

EDIT : the result of F(col) for col in cols will be the the value of edit_jan to edit_des,this is the example result :

using 0 if F(col) is None else F(col) for col in cols or F(col) for col in cols if F(col) is not None yield the same result

for data in lrpa:
    print(data.prk.no_prk, data.sum_edit, data.edit_jan, data.edit_okt)

#RESULT
2019.USLS.36.002 None None 112
2019.USLS.64.001 None None 123
2019.USLU.1.002 None None None
2021.USLW.202.003 None None None
2022.USLW.291.001 None None None
2019.USLS.63.001 None None None
2019.USLU.12.002 None None None
2019.USLU.13.002 None None None
2020.USLS.148.003 None None None
2021.USLU.24.013 None None None
2021.USLW.15.006 None None None
2021.USLW.208.003 None None None
2022.USLW.152.001 None None None
2022.USLW.296.001 None None None

1 Answers1

0

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.)

J_H
  • 17,926
  • 4
  • 24
  • 44
  • i already tried this but it still return None. – Andi Fathul Mukminin Sep 07 '22 at 02:30
  • Show us the result of the list comprehension `[F(col) for col in cols if F(col)]`. Also, note that the expression `0 if F(col) is None else F(col)` will map nones to zeros. – J_H Sep 07 '22 at 21:28
  • Edited my question with result example, unfortunately its still not working – Andi Fathul Mukminin Sep 08 '22 at 01:56
  • No one who reads your question can run what you run, to reproduce your results. https://stackoverflow.com/help/minimal-reproducible-example , https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – J_H Sep 11 '22 at 23:07