1

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.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
Tessnim
  • 434
  • 9
  • 29

1 Answers1

1

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.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
  • thank you for this!! Another solution I was working on was to remove readonly=True from the field in my model, set it invisible in my xml, and add another field related to this one. It totally worked but your solution is a lot simpler. Thanks again. – Tessnim Aug 10 '21 at 14:17
  • I'm working on odoo 14 by the way but I didn't find a tag for odoo14 – Tessnim Aug 10 '21 at 14:19
  • 1
    i've added all "missing" tags. so this question can be answered with this answer on all the tagged versions – CZoellner Aug 10 '21 at 14:40