0

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.

Joozer
  • 25
  • 6

1 Answers1

0

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
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • hi Kenly, thanks for answer. I tried but no luck. it is empty value. so how can i get id value from sml.picking_id.partner_id? i set up sml.picking_id.partner_id.id but show False only. – Joozer Aug 30 '20 at 07:41
  • You need to [recompute](https://stackoverflow.com/questions/34089393/how-to-recompute-stored-functional-field-values-in-odoo) the values for the already created records. – Kenly Aug 30 '20 at 08:49
  • it should be self.recompute rgt? need to add end of compute function? – Joozer Aug 30 '20 at 09:38
  • No, you need to execute ``recompute`` once. the compute method will trigger for new records when the ``picking_id`` change. – Kenly Aug 30 '20 at 09:42
  • how can i exactly do ? can you give me example base on my code. – Joozer Aug 30 '20 at 09:48
  • Start Odoo [shell](https://www.odoo.com/documentation/12.0/reference/cmdline.html#shell) and use `self.env`. – Kenly Aug 30 '20 at 10:27
  • Thanks and now I see, is there anyway to avoid recompute ? – Joozer Aug 31 '20 at 03:04
  • The recompute is needed for stored computed fields, When you create a new record its value should be computed and set and if picking_id or partner_id or internal_ref are not set, Odoo should recompute it when one of those values change. In the example, above we only get a field value and set it to `irLot`, we could use a [related field](https://www.odoo.com/documentation/12.0/reference/orm.html#related-fields) instead. – Kenly Aug 31 '20 at 09:18