
Learn how to create line numbers for One2many fields in Odoo using a counter to enhance record organization in sales orders, invoices, and more.
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.
A computed field will be added. It generates line number for each record. This is done in One2many field using a counter.
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
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.
<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.
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:
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.
Your email address will not be published. Required fields are marked *