How to generate the sequence in Odoo.
?In Odoo, a sequence is a record that generates a unique number or string according to a specific pattern. It's commonly used to generate unique identifiers for records like sales orders, purchase orders, invoices, etc. Odoo sequences are highly configurable, allowing for prefixes, suffixes, padding, and date-based sequencing, among other options.
In the data file, you can customize the sequence with fields such as name, code, prefix, padding, and number_increment.
Here is an example of defining a simple sequence for the employee.
<!-- generating sequence -->
<data noupdate="1">
<record id="id" model="ir.sequence">
<field name="name">name</field>
<field name="code">model_name</field>
<field name="prefix">value of prefix</field>
<field eval="1" name="number_next"/>
<field eval="1" name="number_increment"/>
<field name="padding" eval="4"/>
<field eval="False" name="company_id"/>
</record>
</data>
To use the defined sequence in your code, you need to use the next_by_id method of the ir. sequence model. This method retrieves the next number in the sequence.
领英推荐
Description of the Sequence Fields
@api.model_create_multi
def create(self, vals_list):
?"""
?Overridden create() method to add sequence
?------------------------------------------
: param self: object pointer
: param vals_list: List of dictionary containing fields and values
:return: Recordset of the newly created record(s)
"""
seq = self.env.ref('module_name.id_of_generate_sequence')
for vals in vals_list:
vals['emp_reg_no'] = seq.next_by_id()
return super().create(vals_list)
Using this create method, you can generate the employee sequence or you can generate any sequence using this method.