I was looking for a field calculator plugin, but I think none exists.
I would like to have ideas or hints about the best way (simplest is the best) I could develop a plugin that allow user to make calculations on fields using mathematical expression.
For beginning I would just let user type an expression in a form and send it through ajax for instance:
'field4' = ('field1' + 'field2') / 'field3'
Then, I wonder how I could parse such expression (or any other similar) and do the calculation on the server side without bothering with validation for now.
import compiler
def fieldCalculator(request):
queryset = Models.objects.all()
eq = request.GET['expression']
ast = compiler.parse(eq)
#I wonder how to do this part using the parsed expression
for object in queryset:
object.field4 = (object.field1 + object.field2) / object.field3
object.save()
edit:
The expression would be user defined, so can take any number of fields and any mathematical operations (lets stay to the + - * / for now).
The expression in its generic way would be:
'output_field' = 'a mathematical expression using variables (other fields) and constants'