Search code examples
python-3.xodooodoo-11odoo-12

cannot get ID from partner_id.id in Odoo v11


I got some issues on getting id from rest.partner in Odoo. I added compute field in stock.move.line called irLot. below is code sample.

class StockMoveLine(models.Model):
_inherit = 'stock.move.line'

irLot = fields.Char(string="Internal Reference", compute='_compute_ir')


def _compute_generate_lot(self):
    
    partner_id = self.picking_id.partner_id.id
    partner_obj = self.env['res.partner']
    obj = partner_obj.search([('id','=',partner_id)])
    
    for rec in obj:
        internal_ref = rec.ref
         
    self.irLot = internal_ref 

so my problem is when I assign to partner_id variable. there is no value is coming out even though i assigned self.picking_id.partner_id.id. there is always showing False. so I assigned id directly and it works as below.

 partner_obj = self.env['res.partner']
 obj = partner_obj.search([('id','=','112')])

What did I do wrong? quite noob on odoo please suggest me.


Solution

  • irLot depends on the value of picking_id, you need to specify it using api.depends() decorator.

    If it uses the values of other fields, it should specify those fields using depends()

    You can refer to the Computed fields documentation.

    The default picking_id and partner_id are not mandatory, so check if they are set before trying to get their values. self.picking_id.partner_id when it is set is a recordset, you do not need to search using its id, you already have it as a record.

    You can't set the value of a field on a recordset self.irLot = internal_ref, try to loop over self and set the value for each record.

    class StockMoveLine(models.Model):
        _inherit = 'stock.move.line'
    
        irLot = fields.Char(string="Internal Reference", compute='_compute_ir')
    
        @api.depends('picking_id')
        def _compute_generate_lot(self):
            for sml in  self:
                if sml.picking_id and sml.picking_id.partner_id:
                    sml.irLot = sml.picking_id.partner_id.internal_ref