Home Blog How To Create a Sequence Number In Odoo
How To Create a Sequence Number In Odoo

How To Create a Sequence Number In Odoo   

Odoo ERP has become one of the top choices for businesses because of its ability to handle every possible business need. The little services that Odoo provides helps in the growth of the business. Odoo helps in proper management of your records.

It provides us with an option to create our own sequence for naming of the records.

Naming the records in a proper way will help us distinguish similar records from one another just by a look, and by doing so we will be able to neatly manage the storing process of our data. In Odoo, the sale orders and invoices are named using a sequence number.

This helps in identifying a record just by its name. It also provides us with the provision of creating a sequence field for any record that we want.

In this blog, we are going to see how we can generate a sequence number for a field.

The sequential field in Odoo also known as an auto incremental field generates an auto incremental number every time a new record is generated.

If we need to add a sequence field for naming in our custom model, we can do this easily by making the name field incremental. We can also generate a sequence number by creating a new field or inheriting an old field and making that field auto-incremental.

So first of all, we need to create a "data folder" in our module in which we will create an  "ir.sequence" file, which is an xml file and will help in setting up the sequence we want to create for our field.

Here, I have created a custom module name "sequence_order" in which i will create the basic folders and files (‘data, models, views, manifest file, init.py file’) required for creating a module in case we are creating a new module.

For old modules, simply add the ‘ir.sequence’ file to the data folder(create if not present).

How To Generate Sequence Number In Odoo

We can simply add the data file with other ‘.xml’ files in the Views folder but creating a new folder is the proper convention in Odoo and it helps the us in distinguishing and identifying the data file.

In the models.py, we have our custom model "sequence.order".

In this custom model, we’ll create a new field named ‘sequence’. This will be our auto incremental field that we’ll be using to generate a sequence.

After this, we’ll inherit the default create method of Odoo and we’ll add our custom code to it. This custom code includes creating an environment for ‘ir.sequence’ model and calling our custom sequence method here.

------ Code ------


    def create(self, vals):
        vals['sequence'] = self.env['ir.sequence'].next_by_code('sequence.order')
        return super(SequenceOrder, self).create(vals)

How To Generate Sequence Number In Odoo

Next we need to create the record which we are referring to in the create method. This record is created in the data file.

<record id="sequence_order.seq_order" model="ir.sequence">
            <field name="name">Sequence Order</field>
            <field name="code">sequence.order</field>
          <field name="number_next_actual">001</field>
            <field name="prefix">Seq Order </field>
            <field name="padding">1</field>

For the data file, we need to follow these simple steps:

The first thing is setting up the record id and model for the record.

The id is unique id for reference of this record and model remains the same.

Then we set the name of this sequence.

Then the code, the number with which the sequence should start and then the prefix for this number. We can also setup a suffix for the sequence.

How To Generate Sequence Number In Odoo

In the xml file, we can write <data noupdate=”1”> , if we want to make sure that the sequence does not get updated when we update the module.

In the view file of our custom module, we can display this sequence field at the top in the header like this:

          <div class="oe_title">
                      <field name="sequence" readonly="1"/>

  We have used the ‘oe_title’ class of Odoo to define the sequence at the top.

How To Generate Sequence Number In Odoo

Next thing, that we need to make sure is that we call the sequence file from the data folder in the manifest file of Odoo.

How To Generate Sequence Number In Odoo

Now, we’ll install/upgrade the module and after that, we’ll see that our field is being displayed at the top in the form view.

How To Generate Sequence Number In Odoo

This field will be uneditable. We’ll now enter the required information in the record and then click on Save. After that, we’ll see that the record is saved and a sequence is generated. This sequence is auto incremental for each record.

How To Generate Sequence Number In Odoo

We hope this blog helpful to you and able to explain the entire process to Generate or Create Sequence Number in Odoo ERP. In case you have any query related to the process, let us know in the comment section.

                                                                                                                             Code Download From GitHub