2

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 - - -

forvas
  • 9,801
  • 7
  • 62
  • 158

1 Answers1

3

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,
)
forvas
  • 9,801
  • 7
  • 62
  • 158
  • If you have normal field and try to convert into compute you must have to set store=True otherwise field will not store in database. Also related =False not needed in syntax. – Balvant Ramani Oct 22 '20 at 03:28
  • In this case I did not want to store the field in database since the original one was not stored. I had to specify `related=False` to remove the previous related value the original field had. – forvas Oct 22 '20 at 09:01