# Save in the bd a field function

**URL:** https://discuss.tryton.org/t/save-in-the-bd-a-field-function/804
**Category:** Developer
**Created:** [October 15, 2018, 1:54pm UTC](https://discuss.tryton.org/t/save-in-the-bd-a-field-function/804 "2018-10-15T13:54:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![electro1](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/e/439d5e/32.png) [@electro1](https://discuss.tryton.org/u/electro1)
#### Post date: [October 15, 2018, 1:54pm UTC](https://discuss.tryton.org/t/save-in-the-bd-a-field-function/804/1 "2018-10-15T13:54:45Z")

</div>

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 ,
            })

```

---

<div class="post-metadata">

### Author: ![fransuar](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/f/59ef9b/32.png) [@fransuar](https://discuss.tryton.org/u/fransuar)
#### Post date: [October 16, 2018, 4:09am UTC](https://discuss.tryton.org/t/save-in-the-bd-a-field-function/804/2 "2018-10-16T04:09:27Z")

</div>

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:

```

> **[on\_change of fields — Tryton server](https://docs.tryton.org/latest/server/topics/models/fields_on_change.html)**

```
Regards
    Francisco

```

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [October 18, 2018, 9:04am UTC](https://discuss.tryton.org/t/save-in-the-bd-a-field-function/804/3 "2018-10-18T09:04:27Z")

</div>

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`.
