Save in the bd a field function

Hello,

I have created the following field function:

t = fields.Function(fields.Float(‘t’),‘get_t’)

def get_t(self,name = None):
    m = round ((Decimal(self.quantity) * Decimal(self.gross_unit_price)),2)
    return m

I’m trying to record it in a field that I created , but without success Please help

d= fields.Float(‘d’)

def get_top(cls ,name = None):
    if(SaleLine.unit_price):
        cls.write( SaleLine, {
            'd': SaleLine.t ,
            })

Hello electro1,
fields.Function’s are not stored on db. These are dynamic fields
Maybe you should try a fields.Float’s instead a fields.Function, and a on_change_ or an on_change_with_ function
Something like this maybe:

 @fields.depends('quantity', 'gross_unit_price')
 def on_change_with_t(self, name):
     t = round ((Decimal(self.quantity) * Decimal(self.gross_unit_price)),2)
     return t

That will replace the value of t anytime the value of quantity or gross_unit_price change

This could help too:

http://docs.tryton.org/projects/server/en/latest/topics/models/fields_on_change.html?highlight=on_change

Regards
    Francisco

Also the getter method should never modify the data because it is often called in a readonly transaction.
If you want to cache some computed data, the best is to do it at a specific state of the Model, like it is done on Sale.confirm with Sale.store_cache.