Search code examples
pythonxmlodooodoo-10odoo-12

Migration module from 10 to 12


I'm trying to migrate a module from Odoo 10 to 12 but it shows me this error and I don't understand why:

Field 'state' used in attributes must be present in view but is missing

Can you please help me to solve this issue:

Field 'state' used in attributes must be present in view but is missing:
 - 'state' in attrs="{'invisible': ['|','|',('journal_entry_ids', '!=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"
 - 'state' in attrs="{'invisible': ['|','|',('journal_entry_ids', '=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"

Error context:
View `account.bank.statement.form.reconciliation`
[view_id: 1684, xml_id: n/a, model: account.bank.statement, parent_id: 462]
None while parsing /home/PycharmProjects/Odoo12/bank_reconciliation/views/account_view.xml:4, near
<record id="view_bank_statement_form_reconciliation" model="ir.ui.view">
    <field name="name">account.bank.statement.form.reconciliation</field>
    <field name="model">account.bank.statement</field>
    <field name="inherit_id" ref="account.view_bank_statement_form"/>
    <field name="arch" type="xml">
        <data>
            <field name="date" position="after">
                <field name="type" invisible="1"/>
            </field>
            <xpath expr="//button[1]" position="attributes">
                <attribute name="attrs">{'invisible': [('type', '!=', 'cash')]}</attribute>
            </xpath>
            <xpath expr="//field[@name='line_ids']/tree/field[@name='bank_account_id']" position="after">
                <field name="type" invisible="1"/>
                <button name="select_account_move_line" type="object" icon="fa-registered" attrs="{'invisible': ['|','|',('journal_entry_ids', '!=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"/>
            </xpath>
            <xpath expr="//field[@name='line_ids']/tree/field[@name='bank_account_id']" position="after">
                <button name="cancel_reconciliation" type="object" icon="fa-chain-broken" attrs="{'invisible': ['|','|',('journal_entry_ids', '=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"/>
            </xpath>
            <xpath expr="//field[@name='line_ids']" position="inside">
                <form string="Statement Line" create="false">
                    <group col="4">
                        <field name="statement_id"/>
                        <field name="date"/>
                        <field name="name"/>
                        <field name="ref"/>
                        <field name="partner_id"/>
                        <field name="amount"/>
                        <field name="journal_currency_id" invisible="1"/>
                        <field name="sequence"/>
                        <field name="note"/>
                </group>
                    <notebook colspan="4">
                        <page string="Ecritures liées">
                            <field name="move_line_ids">
                                <tree readonly="1">
                                    <field name="name"/>
                                    <field name="account_id"/>
                                    <field name="move_id"/>
                                    <field name="date"/>
                                    <field name="debit" sum="Débit"/>
                                    <field name="credit" sum="Crédit"/>
                                </tree>
                            </field>
                        </page>
                    </notebook>
                </form>
            </xpath>
        </data>
    </field>
</record>

The error says that the attribute 'state' is not present in the parent view, but it is present.

Here is the parent view:

<record id="view_bank_statement_form" model="ir.ui.view">
    <field name="name">account.bank.statement.form</field>
    <field name="model">account.bank.statement</field>
    <field name="priority">1</field>
    <field name="arch" type="xml">
        <form string="Bank Statement">
        <header>
            <field name="all_lines_reconciled" invisible="1" />
            <button name="%(action_bank_reconcile_bank_statements)d" string="Reconcile" type="action" class="oe_highlight" attrs="{'invisible':['|','|',('all_lines_reconciled','=',True),('line_ids','=',[]),('state', '!=', 'open')]}"/>
            <button name="check_confirm_bank" string="Validate" type="object" class="oe_highlight" attrs="{'invisible':['|','|',('all_lines_reconciled','=',False),('line_ids','=',[]),('state', '!=', 'open')]}"/>
            <field name="state" widget="statusbar" statusbar_visible="open,confirm"/>
        </header>
        ...
    </field>
</record>

Solution

  • When you write something like this:

    <xpath expr="//field[@name='line_ids']/tree/field[@name='bank_account_id']" position="after">
        ...
    </xpath>
    

    Everything you add there is related to the comodel of the x2many field line_ids and its tree view. So if you add a new field/button with an attrs parameter, you must check that the attributes on the left of the domains are inside the tree view of line_ids, not inside the form of account.bank.statement.

    So you must add the field state to the tree view of the field line_ids:

    <xpath expr="//field[@name='line_ids']/tree/field[@name='bank_account_id']" position="after">
        <field name="type" invisible="1"/>
        <field name="state" invisible="1"/>
        <button name="select_account_move_line" type="object" icon="fa-registered" attrs="{'invisible': ['|','|',('journal_entry_ids', '!=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"/>
        <button name="cancel_reconciliation" type="object" icon="fa-chain-broken" attrs="{'invisible': ['|','|',('journal_entry_ids', '=', []), ('state', '=', 'confirm'),('type', '!=', 'bank')]}"/>
    </xpath>
    

    By the way, you are repeating the same xpath twice, which is confusing and slower for Odoo, so I grouped it into one which makes more sense.