>
> Could anyone give me a brief, clear example of how to have a calculated field ?
>
> I' m trying to have my first calculated field
>
> I have 2 Date fields and I want my calculated field to be the difference in days between the 2 dates
>
> How do I set that ?
>
> The documentation says that "on_change" is an attribute of the filed, like this
>
> Field.on_change
>
> Does this mean that I should write this ?
> fields.Numeric('duration', digits=(2,), on_change=someDatefield) ?
>
> Can I trust the documentation on this ?
The documentation is correct, but as your interpretation may be wrong, the best way to finish to understand the docs is usually to look for examples in the code.
>
> In the code of the sale.sale model, on_change is not used like that, but it' s quite complicated and I' ma bit overwhelmed
on_change* methods are usually used as the doc says, to update the value of other fields when the one you want changes.
For example on_change_foo() will run each time the field foo changes, and so allow you to update another field 'bar'. in this case you have to add 'bar' (or any other field value you may need for the computation) in the depends for this function like this @fields.depends('bar')
for ex:
@fields.depends('bar')
def on_change_foo(self):
if self.foo:
self.bar = 1
else:
self.bar += 1
Meaning if you change foo and it evaluates to True, 'bar' will be set to 1, otherwise bar will increase by 1.
Here the you have to add in the depends any field you plan to use or modify during the calculations.
It may look as a subtle difference in the beginning for an on_change_with* where you update the value of one field when something else changes on the record.
@fields.depends('bar')
def on_change_with_foo(self, name=None):
if self.bar ==100:
return True
return False
In this case, if bar changes, the method runs and the return value will be updated to foo. Meaning foo will always be False unless bar hits 100.
Here the values in the depends define when the function will run, but also you have to put here any field you use in the calculation.
In both cases you can add several fields to the depends, separated by a comma.
>
> Thanks in advance
>
> --
> You received this message because you are subscribed to the Google Groups "tryton" group.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/tryton/19f48579-b67e-4e42-b3d0-108f7954904d%40googlegroups.com.
[1] -
http://doc.tryton.org/4.6/trytond/doc/ref/models/fields.html?highlight=function#function[2] -
http://hg.tryton.org/modules/sale/file/f4db203a1b9f/sale.py#l147[3] -
http://hg.tryton.org/modules/sale/file/f4db203a1b9f/sale.py#l458--
-------------------------
Karla Mª Stenger Sábat
karla....@gmail.com