Search code examples
pythonxmlauto-incrementodoo-9

auto increment - internal reference odoo9


I want to change the type of the field 'ref' (Internal Reference) to be auto incremented (for example every time I create a new contact my Internal Reference should increase by 1). So first contact should have Internal Reference 1, the second 2, the third 3 and so on...

There are no errors but still the reference field is empty. Have I missed some additional code? Can someone help me?

@api.model
def create(self, vals):
    if vals.get('ref', 'New') == 'New':
        vals['ref'] = self.env['ir.sequence'].next_by_code(
            'res.debt') or 'New'
    return super(Partner, self).create(vals)

And the xml file:

      <record id="your_sequence_id" model="ir.sequence">
          <field name="name">Reference</field>
          <field name="padding">3</field>
          <field name="code">res.debt</field>
      </record>

Example of current result


Solution

  • You don't need the unnecessary if statement, because as you stated in your question you want the reference to autoincrement every time a new user is created. the users can't change the field from the form, this is how you get the next reference in odoo.

    @api.model
    def create(self, vals):
        vals['ref'] = self.env['ir.sequence'].get('res.debt')
        return super(Partner, self).create(vals)