Search code examples
odoo-13qweb

Dynamic default value in tree view


I am trying to add line numbers to sale.order.line in the sale order "tree view". Essentially, when a user adds a new product to a quotation, it should appear with a "line number" as a multiple of 10 (i.e. 10, 20, 30, etc)

I am struggling to find out how to add a "computed" default value to a qweb view.

For example:

<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

The goal is to display order lines, while still allowing users to edit them, but not requiring them to enter them manually.

enter image description here


Solution

  • You should override onchange method for product_id, and set Line no with the length of the lines multiplied by 10, something like this:

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

    I hope this answer could be helpful.