Hi, here's my case. I'm creating serial number for my parking inventory on basis of 'sector_id' which is Floors in my model. Here's how i'm creating automatically serial number whenever inventory is created.
my.py
@api.model
def create(self, vals):
if vals.get('serial_number', _('New')) == _('New'):
sector_id = self.env['sector'].search([('id', '=', vals['sector_id'])])
record = self.env['ir.sequence'].search([('code', '=', 'park.inventory.%s' % (sector_id.code))])
if record:
vals['serial_number'] = self.env['ir.sequence'].next_by_code(record.code) or _('New')
else:
new_record = self.env['ir.sequence'].create({
'name': '%s' % (sector_id.code),
'code': 'plot.inventory.%s' % (sector_id.code),
'prefix': "%s/" % (sector_id.code),
'padding': 3,
'company_id': False
})
vals['serial_number'] = self.env['ir.sequence'].next_by_code(new_record.code) or _('New')
rec = super(ParkInventory, self).create(vals)
return rec
Let me know if u need further help.
Will you please explain in detail how to insert this code ?