calculated fields

50 views
Skip to first unread message

Cato Nano

unread,
Dec 29, 2017, 5:31:18 AM12/29/17
to tryton
Hello,

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 ?

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

Thanks in advance

Karla Stenger

unread,
Dec 29, 2017, 9:06:02 AM12/29/17
to try...@googlegroups.com


2017-12-29 7:31 GMT-03:00 Cato Nano <cato...@gmail.com>:
> Hello,
Hello


>
> 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 ?

You are looking for fields.Function [1]

Here's an example which returns an Integer [2] check for the 'on_change_with_currency_digits' method in it [3]
(This is an on_change_with*, not an on_change*)


>
> 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
Message has been deleted

Cédric Krier

unread,
Jan 2, 2018, 5:55:08 PM1/2/18
to tryton
On 2018-01-02 14:08, Cato Nano wrote:
> Karla,
>
> Il giorno venerdì 29 dicembre 2017 15:06:02 UTC+1, Karla Stenger ha scritto:
> > 2017-12-29 7:31 GMT-03:00 Cato Nano <cato...@gmail.com>:
> > > Hello,
> > Hello
> >
> > >
> > > 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 ?
> >
> > You are looking for fields.Function [1]
>
> Thank you so much for your clarification, I think I understand now
>
> But I'm having a types problem
>
> I have 2 Date fields: startDate and endDate
>
> I want the difference in days
>
> I have this line
>
> return abs((self.endDate - self.startDate).days)
>
> But in the client I get this error
>
> TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
>
> NoneType ? I was expecting my dates to be datetime.date type !

Answered at http://www.tryton.org/~irclog/2018-01-02.log.html#t2018-01-02%2023:48

Please avoid to ask same questions on multiple channel in a so short
time frame.

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Cato Nano

unread,
Jan 2, 2018, 6:04:32 PM1/2/18
to tryton
Il giorno martedì 2 gennaio 2018 23:55:08 UTC+1, Cédric Krier ha scritto:
> Answered at http://www.tryton.org/~irclog/2018-01-02.log.html#t2018-01-02%2023:48
>
> Please avoid to ask same questions on multiple channel in a so short
> time frame.

Sorry !

Karla, tanks fofr your clarrification. very effective !
Reply all
Reply to author
Forward
0 new messages