Search code examples
odoo

Odoo readonly field doesn't save value on onchange


I have a onchange field on Odoo in which saves a float value, but then I need to use this field's value on a method that runs when a button is pressed, but when I try to get this value through self.field_name, the value comes equal to 0.

Anyone knows how can I get the field value?


Solution

  • What was happening is that apparently on Odoo you can't write a value to a readonly field, so on my XML I doubled my field that was readonly, and the new one I made it just invisible, and now it works perfectly. This is something that I also discovered, you don't need to declare a new field on the .py file and create it on the XML, you can just copy and paste your field, remove the readonly attribute, and add an invisible attribute, works perfectly.

    The field I'm talking about is the consolidated_balance, and my XML is like this:

    <record id="projected_cash_flow_wizard" model="ir.ui.view">
        <field name="name">projected.cash.flow.wizard</field>
        <field name="model">projected.cash.flow</field>
        <field name="arch" type="xml">
            <form string="Projected Cash Flow">
                <group col="4" colspan="4">
                    <field name="start_date" required="1"/>
                    <field name="final_date" required="1"/>
                </group>
    
                <group col="4" colspan="4">
                    <field name="journal_id" attrs="{'readonly': [('all_journals','=',True)], 'required': [('all_journals','=',False)]}" />
                    <field name="all_journals" />
                </group>
    
                <group col="4" colspan="4">
                    <field name="print_bool" />
                    <field name="consolidated_balance" readonly="1" />
                    <field name="consolidated_balance" invisible="1" />
                </group>
    
                <footer>
                    <button name="process_projected_cash_flow" string="Confirm" type="object" class="btn-primary" />
                    or
                    <button string="Cancel" class="btn-default" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>