How to create sequence number in odoo ?
The creation of Sequence numbers is a very simple process. We can identify each record using its sequence number, which will be unique for each record.
Sequence number means unique identity. It generated a unique number.
When you write any number then automatically generate the number and above display this number .
Enter any number before you can see DEFAULT in place of number.
But when you enter that time new is replaced with the enter number.
Example:
you enter any name that displays 001.
And second time display 002.
And next time display 003.
So, its call sequence.
For this , first create one folder in your module and give it a name like Data folder.
Create one xml file and give a name it , sequence_data.xml
After this write below code into the xml file,
Data (folder)
.xml ( in file)
<odoo>
<record id="sequence_restaurant_menu" model="ir.sequence">
<field name="name">Restaurant Menu Order</field>
<field name="code">restaurant.menu</field>
<field name="prefix">RM/%(year)s/</field>
<field name="padding">4</field>
</record>
</odoo>
Understanding syntax:
1 ) <record id="sequence_restaurant_menu" model="ir.sequence">
here give any name of id and write ir.sequence in the model
2) <field name="code">restaurant.menu</field>
Here we can write any name into the code but to make things easy, we write the model name in the code name.
3) <field name="prefix">RM/%(year)s/</field>
Prefix is used to add any words in front of sequence value.
4) <field name="padding">4</field>
padding is used to give a range of sequence numbers like 4 digit , 5 digit etc.
After this thing , add a field in the model in which we want to create sequence value.
For example,
number = fields.Char(string=“Number”,default=“New”,readonly=True)
Now add this field in the xml file of that model
This thing we have to add in the sheet and above the group.
<div class="oe_title">
<field name="number" style="font-size: 22px;font-weight: bold;"/>
</div>
After this , we have to make the create method into the model in which we want to make sequence values.
.py file
@api.model
def create(self,vals):
vals['number']=self.env['ir.sequence'].sudo().next_by_code('restaurant.menu') or 'New'
res = super(Menu,self).create(vals)
return res
Here we have to write the model name is ir.sequence and give the code name that we write in the sequence.xml file.
So By this method we can generate the sequence value.
For more info , refer to the restaurant module. And the menu model of this module.
How to create sequence number in odoo ?