树视图中的动态默认值

Dynamic default value in tree view

我正在尝试将行号添加到销售订单“树视图”中的 sale.order.line。本质上,当用户将新产品添加到报价单时,它应该以 10 的倍数(即 10、20、30 等)

的形式出现

我正在努力寻找如何将“计算的”默认值添加到 qweb 视图。

例如:

<tree
    string="Sales Order Lines"
    editable="bottom"
    decoration-info="(not display_type and invoice_status == 'to invoice')"
>
    <control>
        <create name="add_product_control" string="Add a product"/>
        <create name="add_section_control" string="Add a section" context="{'default_display_type': 'line_section'}"/>
        <create name="add_note_control" string="Add a note" context="{'default_display_type': 'line_note'}"/>
    </control>
    <field name="sequence" widget="handle" />
    <field name="line_number" default="len(record.order_line_ids) * 10 + 10"/> <--- HERE

目标是显示订单行,同时仍允许用户对其进行编辑,但不要求他们手动输入。

您应该重写 product_idonchange 方法,并将行的长度乘以 10 设置 Line no,如下所示:

@api.onchange('product_id')
def _onchange_product_id(self):
    if self.product_id:
        self.line_number = len(self.sale_id.order_line) * 10

希望这个回答对您有所帮助。