Generate Line Numbers for One2many Fields in Odoo

Abid Patel
06-Nov-2024 Updated : 06-Nov-2024

Learn how to create line numbers for One2many fields in Odoo using a counter to enhance record organization in sales orders, invoices, and more.

Generate Line Numbers for One2many Fields in Odoo

In certain cases such as sales orders or invoices an addition of simple counter for line numbers can enhance accessibility. It can also lead to more organization. This is achievable by incrementing a counter. We do this within a computed function.

Step 1: Put a Computed Field for Line Number with a Counter.

A computed field will be added. It generates line number for each record. This is done in One2many field using a counter.

Code Example:-

python

    from odoo import models, fields, api
    class SaleOrderLine(models.Model):
        _inherit = 'sale.order.line'
        line_number = fields.Integer(string="Line Number", compute="_compute_line_number", store=True)
        @api.depends('order_id.order_line')
        def _compute_line_number(self):
            for order in self.mapped('order_id'):
                sl_no = 1  # Start counter at 1
                for line in order.order_line:
                    line.line_number = sl_no
                    sl_no += 1  # Increment counter for each line
    

Explanation of Code

  • ▹ Define line_number Field: This field is an integer. It holds line number. How? By means of computed function _compute_line_number.
  • ▹ Initialize Counter: We set sl_no as 1 at the start of each order. This is the first line number.
  • ▹ Increment Counter: Every line is assigned line_number. It will equal sl_no. After this, sl_no is incremented by 1. It ensures that the next line will receive next number.

Step 2: Display Line Numbers in Tree View

To bring line numbers into sight in user interface we will append field line_number to One2many field view. This gets done in sales order or invoice form.

XML Code to Modify View

xml

    <record id="view_order_form" model="ir.ui.view">
        <field name="name">sale.order.form.inherit.line_number</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='order_line']/tree" position="inside">
                <field name="line_number"/>
            </xpath>
        </field>
    </record>
    

We inherit sale order form view. Line_number field is added to tree view in order_line. This configuration results in line numbers being displayed directly in interface.

Why Use Line Numbers with a Counter?

Using an incremented counter gives a simple and intuitive method. It adds a unique identifier to each line item. This way is particularly useful for:

  • ▹ User Experience: Easier for users to refer to specific line items.
  • ▹ Readability: It organizes data for users. It makes the One2many field cleaner and more informative.
  • ▹ Consistency: This method ensures that each new line item always receives a unique number. This happens within its document.

Conclusion

Adding line numbers to One2many fields in Odoo is simple. A simple counter suffices. It is possible to enhance usability. Also create better-organized interface for any Odoo model. The model contains multiple lines. Follow these steps. It is straightforward. All these can be done easily. The end result is a more organized interface. Particularly beneficial when you are handling multiple lines.

Make a Comment

Your email address will not be published. Required fields are marked *