Search code examples
python-2.7odooopenerp-7odoo-8

How to use related fields (fields.related) in odoo-8?


I am trying to retrieve comment field(customer internal notes) from res_partner to account invoice module.Right now I just want to print it later I will include it in xml code. I tried in three ways like this,

1)comment2 = fields.Char(string='Comment',related='res_partner.comment',compute='_compute_com')
@api.multi
def _compute_com(self):
    print self.comment2

2)comment = fields.Many2one('res.partner','Comment',compute='_compute_com')
  @api.multi
  def _compute_com(self):
    print self.comment

3)partner_comment = fields.Char(compute='_compute_com')
 @api.multi
 def _compute_com(self):
    Comment = self.env['res.partner'].browse(partner_id).comment
    print Comment

Solution

  • You should use a related field instead:

    comment = fields.Char(related='partner_id.comment')
    

    If you need to store it in your account_invoice record you also need to add the parameter store=True Problem is, this way you can't just print it but if you need to show it you need to put it into your view.

    If you really need to print it temporarly you need to do this other way:

    comment = fields.Char(compute='_compute_comment')
    
    def _compute_comment(self):
        for record in self:
            record.comment = partner_id.comment
            print record.comment