Reputation: 271
How to create a dynamic sequence based on Many2one fields, instead of fixed values.
For example, student registration number based on degree, batch and year. How can I implement this using a sequence in Odoo 10?
Upvotes: 1
Views: 1284
Reputation: 10179
You can make a dynamic sequence following the next steps. I'd have been more accurate if you had given more details about your code, but I think you'll understand the idea to manage your purpose.
Create a sequences.xml file in data folder, and add the file to the __openerp__.py
:
<record id="seq_type_student_registration_number" model="ir.sequence.type">
<field name="name">Student registration number</field>
<field name="code">student.registration.number</field>
</record>
<record id="seq_student_registration_number" model="ir.sequence">
<field name="name">Student registration number</field>
<field name="code">student.registration.number</field>
<field name="prefix">DEGREE/BATCH/%(y)s</field>
<field name="padding">3</field>
</record>
In your *.py file, inherit from your model to modify its create
method:
@api.model
def create(self, vals):
record = super(YourModel, self).create(vals)
sequence = self.env['ir.sequence'].next_by_code(
'student.registration.number') or '/'
sequence = sequence.\
replace('DEGREE', record.degree_id.name).\
replace('BATCH', record.batch_id.name)
record.write({
'your_field': sequence,
})
Upvotes: 2