Search code examples
inheritanceodoosequence

Trouble adding a sequence field to inherited Odoo model


Im trying to add a sequence field to the crm.lead model. I have implemented this code for another custom module I built previously with no issues.

Ive added the new field

 class vox_crm_register(models.Model):
   
    _inherit = 'crm.lead'

    name_seq = fields.Char(
        string='Registration Reference',
        required=True,
        copy=False,
        readonly=True,
        index=True,
        default=lambda self: _('New'))

Ive overwritten the create function

@api.model
    def create(self, vals):
        print(vals.get('name_seq'))
        if vals.get('name_seq', _('New')) == _('New'):
            print('IF is true')
            vals['name_seq'] = self.env['ir.sequence'].next_by_code('vox_crm_register.sequence') or _('New')
        res = super(vox_crm_register, self).create(vals)
        return res

and this is my sequence

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="1">

        <record id="registration_sequence" model="ir.sequence">
            <field name="name">Registration Sequence</field>
            <field name="code">vox_crm_register.sequence</field>
            <field name="prefix">reg</field>
            <field name="padding">3</field>
            <field name="company_id" eval="False"/>
        </record>

    </data>
</odoo>

and heres the view

<xpath expr="//sheet/group" position="replace">
     <div class="oe_title">
         <h1><field name="name_seq"/></h1>
     </div>
</xpath>

the result from the print statements in the create function are

None

IF is true

because its inherited do I have to force it in someway or add a context???

Any help appreciated


Solution

  • The name_seq is not present in the view definition.

    From constrains documentation:

    @constrains will be triggered only if the declared fields in the decorated method are included in the create or write call. It implies that fields not present in a view will not trigger a call during record creation.

    You can deduct that if the field is not present in a view, its value will not be present in the create or write method.

    Edit:

    When you click on the Save button, the saveRecord function will be called to return the list of field names whose values have been modified, the same list will be used in the create method.

    Since the name_seq field is read-only, it will not be present in the create values.