Question About Calculation Fields

Hi, I am making a calculation in Senaite, but I seem to be getting an error when I place this in the calculation:

if [Ca] > 10:
  return [Ca] + [Mg]
elif [Ca] < 10:
  return [Ca] - [Mg]

I set Ca as 11 & Mg as 9 for the test calculation, but it says:

Unspecified exception: invalid syntax
There are currently no items in this folder.

Can someone help me with this problem? Thank you very much!

This is too much code, you have to create one single expression here.
I’d try this:

[Ca] + [Mg] if [Ca] > 10 else [Ca] - [Mg]

For more complex calculations you have to create user-defined functions as external calculations, in your own add-on.
See

But, you cannot use placeholders like [Ca] inside them.

1 Like

Oh my, thank you so much for this! I cannot believe I missed the 2nd part of the page you shared because I went through Senaite’s Quickstart guide.

Thank you again! Will ask if I have more questions