Rewrite an OpenERP 7 method to Odoo 8 syntax?

I have the following OpenERP 7 method:

#   Function to get the vat number (CIF/NIF) and then show it on invoice form view
def _get_vat_num(self, cr, uid, ids, field_name, args=None, context=None):
    partner_pool = self.pool.get('res.partner')
    invoice_pool = self.pool.get('account.invoice')
    res = {}
    for inv in self.browse(cr, uid, ids, context=context):
        invoice = invoice_pool.browse(cr,uid, inv.id, context=None)
        partner = partner_pool.browse(cr, uid, invoice.partner_id.id, context=None)
        res[inv.id] = partner.vat

    return res

inv_vat = fields.Char(compute='_get_vat_num', string="CIF/NIF")

I need to rewrite it to Odoo v8 syntax. I have tried but it doesn't work:

def _get_vat_num(self):
    partner_pool = self.env['res.partner']
    invoice_pool = self.env['account.invoice']

    res = {}
    for inv in self.browse(self.id):
        invoice = invoice_pool.browse(inv.id)
        partner = partner_pool.browse(invoice.partner_id.id)
        res[inv.id] = partner.vat

    return res

What should be the correct code?

Upvotes: 0

Views: 111

Answers (1)

alexbclay
alexbclay

Reputation: 1429

It looks like you're setting a functional field. You should instead be able to define the field as a related field like so:

inv_vat = fields.Char(string="VAT", related="partner_id.vat")

If you really want it as a functional field, this is how you would do it

inv_vat = fields.Char(string="VAT", compute="_get_vat_num")

def _get_vat_num(self):
    # self is a recordset of account.invoice records
    for invoice in self:
        # to set a functional field, you just assign it
        invoice.inv_vat = invoice.partner_id.vat

Check out the recordset documentation: https://www.odoo.com/documentation/8.0/reference/orm.html#recordsets

And the computed fields documentation: https://www.odoo.com/documentation/8.0/reference/orm.html#computed-fields

Upvotes: 1

Related Questions