Search code examples
pythonodooodoo-8

How to make a button appear in condition in Odoo


I create a module that inherit module stock.inventory, I want the button "1st validate" only appears when "real quantity" is different from "theoretical quantity". Here is the inteface: enter image description here

Here is my xml code:

    <record id="linh_view_inventory_form_ext" model="ir.ui.view">
            <field name="name">linh.view.inventory.form.ext</field>
            <field name="model">stock.inventory</field>
            <field name="inherit_id" ref="stock_account.view_inventory_form_inherit"/>
            <field name="form">form</field>
            <field name="arch" type="xml">

                 <button name="action_cancel_inventory" states="confirm" string="Cancel Inventory" type="object" position="before">     
                    <button name="inventory_1st_validate" states="need_validate" string="1st Validate" class="oe_highlight" type="object" attrs="{'invisible':[('theoretical_qty','=','product_qty')]}/>           
                 </button>

                <field name="state" position="replace">
                    <field name="state" widget="statusbar" statusbar_visible="draft,confirm,acct_validated,done"/>              
                </field>
            </field>
    </record>   

But I got this error:

Error: Unknown field theoretical_qty in domain [["theoretical_qty","=","product_qty"],["state","not in",["need_validate"]]]

"theoretical_qty" is the field name of "theoretical quantity" which I got from model "stock.picking". I don't know why the view doesn't recognize it.

Thank you very much for your help.


Solution

  • theoretical_qty is the field of stock.inventory.line you can't use directly inside stock.inventory .

    you can use it in tree/form view of stock.inventory.line only.

    One more thing , for using a field inside attrs it must be present on view also .

    (either in the current view or inherited parent view ).