Skip to Content
Menu
This question has been flagged
1 Reply
25312 Views

Hello,

i'm using odoo10 community

i'm customizing the partner form for adding custom fields, to do that i did go to "developer mode" and than modify the "res.partner" module by adding custom fields, i created Float,chat, text, int without problems.

than to show the new customized fields i did go in "user interface" -> "views" and modified the "res.partner.form" by adding the field in the place i wanted.

during fields creation i discovered that is possible to compute fields, but i'm not getting exactly how to do it, in the Field creation form there's an "advanced properties" and an area where to add the "computing code", so i did some search but i did not find enough infos...that's why i'm posting here...

i'd like to make a computed field to calculate the total sum of 4 other fields, will this value be possible to be shown immediatly after 4 fields filling? will this "sum" be stored in database or it will stay on the fly? when i will check the partner view there's still be that computed field with the SUM value?

i'm using this code, added directly inside the "compute" area in the field creation form, all fields, sum and other 4 are float.

@api.one

@api.depends('res.partner')

    def _compute_totale(self):
    x_expenseTotal = 0   

    x_expenseTotal = x_expenseOFF1 + x_expenseOFF2 + x_expenseON1 + x_expenseON2   

self.totale = x_expenseTotal



Avatar
Discard
Best Answer

Hi, I will explain you with an example


@api.depends('variable', 'operator', 'max_value', 'list_base_price', 'list_price', 'variable_factor')
def _get_name(self):
for rule in self:
name = 'if %s %s %s then' % (rule.variable, rule.operator, rule.max_value)
if rule.list_base_price and not rule.list_price:
name = '%s fixed price %s' % (name, rule.list_base_price)
elif rule.list_price and not rule.list_base_price:
name = '%s %s times %s' % (name, rule.list_price, rule.variable_factor)
else:
name = '%s fixed price %s and %s times %s Extra' % (name, rule.list_base_price, rule.list_price, rule.variable_factor)
rule.name = name

#fields
name = fields.Char(compute='_get_name', store=True)


The compute fields compute values on the fly, that means while rendering the view which compute field contains, If you need to store the value, simply put 'store = True' while defining fields. You can give @api.one or @api.multi depending on recordset so you can directly access values using self. Using @api.depends you can give depending fields or models.

Avatar
Discard