简体   繁体   English

如何在 Odoo 13 中将现有相关字段转换为计算字段?

[英]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.我想继承它,删除related参数并添加一个compute一个以根据某些条件设置其值。

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 - - -断言错误:没有选择的字段 stock.move.line.picking_code - - -

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.解决了,如果有人对这个主题感兴趣,它是一个Selection字段,所以如果删除related参数,我必须再次指定selection参数的元组列表。

picking_code = fields.Selection(
    selection=[
        ('incoming', 'Receipt'),
        ('outgoing', 'Delivery'),
        ('internal', 'Internal Transfer'),
    ],
    compute='_compute_picking_code',
    related=False,
    readonly=True,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM