I am trying to create a sequence that would be on the form_serial_no field of a formdownload model at the click of save button. This form_serial_no field will pick the company_short_code field of companyname model in the same models.py file and padded it with seven(7) digit number e.g CHN0000001 where CHN is the value of company_short_code field and 0000001 is the first sequence of the record. Below are my code snippets:
models.py code
class CompanyName(models.Model):
_name = 'companyname'
_rec_name = 'company_name'
company_name = fields.Char(string="Company Name", required=True)
company_short_code = fields.Char(string="Company short code", required=True)
class FormDownload(models.Model):
_name = 'formdownload'
name = fields.Many2one('companyname', string="Company Name", ondelete='cascade',
required=True)
form_serial_no = fields.Char(string="Form Serial No", readonly=True)
status = fields.Boolean(string="Status", default=False)
@api.model
def create(self, vals):
vals['form_serial_no'] = vals['name']
if vals:
vals['form_serial_no'] = self.env['ir.sequence'].get('formdownload')
return super(FormDownload, self).create(vals)
sequences.xml code
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data noupdate="1">
<!-- Sequence for form download serial number -->
<record id="ref_code_form_serial_no" model="ir.sequence.type">
<field name="name">Sequence for form download serial number</field>
<field name="code">formdownload.form_serial_no</field>
</record>
<record id="seq_form_serial_no" model="ir.sequence">
<field name="name">Sequence for form download serial number</field>
<field name="code">formdownload.form_serial_no</field>
<field name="prefix">company_short_code</field>
<field name="padding">7</field>
<field name="company_id" eval="False"/>
</record>
</data>
</openerp>
When i check the formdownload table there is no record created for form_serial_no field. Kindly help me look into this.
@Emipro Technologies answer should actually work for you because you got the sequence code wrong.
I suspect two reasons why it's not working:
You haven't included sequences.xml
in your __openerp__.py
file so a call to
name=self.env['ir.sequence'].get('formdownload.form_serial_no')
will return False
because the sequence doesn't exist. In other words check "Sequences & identifiers => Sequences" if it really exists in the database.
change <data noupdate=" 1">
to with noupdate set to 1, This means that the xml stored in the database will never be updated once it's created, so even if you change the sequence it won't get updated and you would be working with old code
Some other suggestions.
I can see you're trying to create a dynamic field that picks the company short code and attaches it to the sequence, The sequence prefix takes a Fixed Value and can't be dynamic, the only way to have a dynamic sequence AFAIK is to do it in the create method like this:
@api.model
def create(self, vals):
serial_no = self.env['ir.sequence'].get('formdownload.form_serial_no')
code = self.env['companyname']. \
browse(vals['name']).company_short_code
# merge code and serial number
vals['form_serial_no'] = code + serial_no
return super(FormDownload, self).create(vals)
You also don't need to check vals
if there's any value. it would always be set in the create method. so the extra check is useless here.