Search code examples
odooodoo-11odoo-12odoo-13odoo-14

cannot find out why odoo sequence is incrementing twice?


I created a model with a sequence field on it:

class AimedPurchaseOrder(models.Model):
    _name = "aimed.purchase.order"

    name = fields.Char('Number', readonly=True, copy=False, 
                    default=lambda self:self.env['ir.sequence'].next_by_code('aimed.purchase.order'))

And I added the sequence like this:

<record id="seq_purchase_order" model="ir.sequence">
    <field name="name">Aimed Purchase Order</field>
    <field name="code">aimed.purchase.order</field>
    <field name="prefix">BCV</field>
    <field name="padding">4</field>
    <field name="company_id" eval="False"/>
</record>

The problem is that when I open the form view to create a new record, I get the next value of the sequence that it should be saved, but when I save the record it increments another time and I find records in the tree view with this sequence :

BCV0002, BCV0004, BCV0006, ...

How it that possible? How can I fix it so it saves the default value showed at first time without incrementing again?

Update:

When I remove the readonly=True it works fine, but I can't let this field editable.


Solution

  • I'm not fully sure but IIRC you can use force_save="1" since Odoo 11 as attribute on fields in views to tell Odoo to write values for readonly fields.

    An example for a readonly field:

    <field name="my_readonly_field" readonly="1" force_save="1" />
    

    If on older Odoos (10 and less) there is a community module adding this feature to Odoo which is called web_readonly_bypass and for example can be found here for Odoo 10.