Integrated Tech Solutions

How to create a Sequence field in Odoo

By - Admin June 28, 2023

To create a sequence field in Odoo, you need to define a new field of type “Char” or “Integer” and set its value using a sequence. Here’s a step-by-step guide on how to create a sequence field in Odoo:

Step 1: Identify the model Determine the model where you want to add the sequence field. For example, let’s say we want to add a sequence field to the “sale.order” model.

Step 2: Define the sequence Open the XML file corresponding to the model you identified in the previous step. Within the <data> tag, define a new sequence using the <record> element. Here’s an example:

<record id="sequence_sale_order" model="ir.sequence">
    <field name="name">Sale Order Sequence</field>
    <field name="code">sale.order.sequence</field>
    <field name="prefix">SO/</field>
    <field name="padding">5</field>
</record>

In this example, we’ve defined a sequence with the name “Sale Order Sequence” and the code “sale.order.sequence”. The prefix is set to “SO/” and the padding is set to 5, which means the sequence values will have leading zeros to reach a length of 5 characters.

Step 3: Add the sequence field to the model Within the same XML file, locate the <field> tag where you want to add the sequence field. Add a new field with the desired name and type, and set its value using the sequence. Here’s an example:

<record id="view_sale_order_form_inherit" model="ir.ui.view">
    <field name="name">sale.order.form.inherit</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="arch" type="xml">
        <form>
            <field name="name"/>
            <field name="sequence_number" widget="char" readonly="1"/>
            <!-- Other fields -->
        </form>
    </field>
</record>

In this example, we’ve added a new field called “sequence_number” of type “Char” to the “sale.order” model’s form view. The widget attribute is set to “char” to display the sequence value as a string. The readonly attribute is set to “1” to prevent editing the sequence field manually.

Step 4: Set the sequence value in the model In the Python code of the model (Python file), override the create() method to set the sequence value when a new record is created. Here’s an example:

pythonCopy codefrom odoo import models, fields, api

class SaleOrder(models.Model):
    _inherit = 'sale.order'
    
    sequence_number = fields.Char('Sequence Number', readonly=True)
    
    @api.model
    def create(self, vals):
        if 'sequence_number' not in vals:
            vals['sequence_number'] = self.env['ir.sequence'].next_by_code('sale.order.sequence') or '/'
        return super(SaleOrder, self).create(vals)

In this example, we’ve added the sequence_number field to the model and overridden the create() method. Inside the method, we check if the sequence_number field is present in the vals dictionary (values passed during record creation). If it’s not present, we generate the next sequence value using the next_by_code() method of the ir.sequence model and assign it to the sequence_number field.

Step 5: Update the module After making the changes, update your Odoo module and restart the Odoo server. You can do this by upgrading the module or restarting the server, depending on your deployment.

Once the module is updated and the server is running, the sequence field should be visible in the specified view. When creating a new record, the create() method will automatically assign the next sequence value.

Note: This is a basic example to demonstrate the concept of a sequence field in Odoo. You can customize the sequence options, such as prefix, padding, and sequence code, according to your specific requirements.

Keep Reading

👋 Hi, Find this Helpful? There is More

You Asked,
We made it!

fix japanese keyword hack

Step by Step Video Tutorials on trending topics in software development

Yes I am Curious »

5 Tips to Pick the Most Engaging Live Chat for Your Website

In today’s fast-paced digital world, providing excellent customer service is a key differentiator for businesses. One of the most effective...

Know More »
next js deployed on aws

Beginners Guide to deploy NextJs App on AWS EC2 Server

In this blog post, we will walk you through the step-by-step process of deploying a Next.js application on an AWS...

Know More »