Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an existing related field into a computed one in Odoo 13?

I have this existing field:

picking_code = fields.Selection(
    related='picking_id.picking_type_id.code',
    readonly=True,
)

And I want to inherit it, remove the related parameter and add a compute one to set its value depending on some conditions.

My attempt:

@api.depends('picking_id', 'picking_id.picking_type_id',
             'picking_id.picking_type_id.code',
             'move_id', 'move_id.picking_type_id',
             'move_id.picking_type_id.code')
def _compute_picking_code(self):
    _logger.critical('NEVER EXECUTES THIS' * 80)
    for line in self:
        if line.picking_id:
            line.picking_code = line.picking_id.picking_type_id.code
        else:
            line.picking_code = line.move_id.picking_type_id.code

picking_code = fields.Selection(
    related=False,
    compute='_compute_picking_code',
)

The problem is that the compute method is never executed and I get the following error, which makes sense since if the compute method is not executed, no selection value is set to the field:

AssertionError: Field stock.move.line.picking_code without selection - - -

like image 890
forvas Avatar asked Dec 05 '25 18:12

forvas


1 Answers

Solved, if anyone is interested on the subject, it is a Selection field, so if remove the related parameter I have to specify again the list of tuples for the selection parameter.

picking_code = fields.Selection(
    selection=[
        ('incoming', 'Receipt'),
        ('outgoing', 'Delivery'),
        ('internal', 'Internal Transfer'),
    ],
    compute='_compute_picking_code',
    related=False,
    readonly=True,
)
like image 102
forvas Avatar answered Dec 08 '25 07:12

forvas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!