Search code examples
python-2.7odooodoo-9

Odoo 9 how to correctly concat two chars?


I have 3 fields and trying to concat fields so char3=char1+char2

Wrote the following code:

# -*- coding: utf-8 -*-

from openerp import models, fields, api
from openerp.tools import ustr

class pucrhase_order_pref_supplier(models.Model):
    _inherit = 'purchase.order.line'
#this field will be displayed on the product list in the purchase order
    preferred_supplier_product = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
    preferred_supplier_template = fields.Char(related="product_id.preferred_supplier_middle", string="Preferred Supplier", readonly="true")
    preferred_supplier = fields.Char(compute='_onchange_proc', store="True")

@api.one
@api.depends('preferred_supplier','preferred_supplier_product','preferred_supplier_template')
def _onchange_proc(self):
        string1 = self.preferred_supplier_product
        string2 = self.preferred_supplier_template
        output = ustr(string1)+"-"+ustr(string2)
        self.preferred_supplier = output

Not sure why but preferred_supplier is not calculated (other fields works fine). Should I use onchange instead?


Solution

  • You can concat two string by following method:

    @api.onchange('char1', 'char2')
    def _onchange_weight_range(self):
        list = []
        if self.char1:
            conc = str(self.char1 or '') + '-' + str(self.char2 or '')
            list.append(conc)
            self.char3 = ', '.join(list)