How to generate the sequence in Odoo.

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

  • name: A human-readable name for the sequence.
  • code: A unique identifier used in code to reference this sequence.
  • prefix: A string added before the number (can include placeholders for dynamic elements like the current year).
  • padding: The total length of the numeric part of the generated sequence, padded with leading zeros if necessary.
  • number_increment: The step between consecutive numbers in the sequence.
  • noupdate: When set to "1", prevents the sequence from being automatically updated during module updates, which is useful for preserving the sequence across updates.

@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.


要查看或添加评论,请登录

Srujan Raval的更多文章

社区洞察

其他会员也浏览了