Search code examples
pythonxmlpython-3.xodooodoo-11

What is the "Fields view get" option in the Odoo 11 developer menu?


I am trying to debug a module by using the developer mode and the menu that can be opened using the bug symbol. There is a menu item "Edit form view" which is quite handy if you want to look at the source code of forms. There is also the menu item "Fields view get" which shows the same form in a slightly different way.

I do not understand where the extra items come from. There are several additional attributes in the field definition and often there is the item modifiers="{...}".

Where do these additional attributes come from?

Example code from a form that defines a partner:

fields view get

<form string="Partner" modifiers="{}">
<sheet modifiers="{}">
    <div class="oe_button_box" name="button_box" modifiers="{}">
        <button class="oe_stat_button o_res_partner_tip_opp" type="action" attrs="{'invisible': [('customer', '=', False)]}" name="273" icon="fa-star" context="{'search_default_partner_id': active_id}" modifiers="{'invisible':[['customer','=',false]]}" options="{}">
            <field string="Verkaufschancen" name="opportunity_count" widget="statinfo" modifiers="{'readonly':true}"/>
        </button>

edit form view

<form string="Partners">
            <sheet>
                <div class="oe_button_box" name="button_box">
                    <button name="toggle_active" type="object" class="oe_stat_button" icon="fa-archive">
                        <field name="active" widget="boolean_button" options="{&quot;terminology&quot;: &quot;archive&quot;}"/>
                    </button>

Solution

  • About fields view_get

    Every model of Odoo has a fields_view_get method which you can overwrite. this method is executed once the XML code of the view is loaded and before being rendered to HTML. This means that you can do some dynamic modifications in the views. Look for def fields_view_get in the Odoo modules, you will find a lot of cases. An example:

    @api.model
    def fields_view_get(self, view_id=None, view_type='form', toolbar=False,
                        submenu=False):
        result = super(AccountMoveLine, self).fields_view_get(view_id,
                                                              view_type,
                                                              toolbar=toolbar,
                                                              submenu=submenu)
    
        doc = etree.XML(result['arch'])
        if view_type == 'tree' and self._module == 'account_payment_order':
            if not doc.xpath("//field[@name='balance']"):
                for placeholder in doc.xpath(
                        "//field[@name='amount_currency']"):
                    elem = etree.Element(
                        'field', {
                            'name': 'balance',
                            'readonly': 'True'
                        })
                    orm.setup_modifiers(elem)
                    placeholder.addprevious(elem)
            if not doc.xpath("//field[@name='amount_residual_currency']"):
                for placeholder in doc.xpath(
                        "//field[@name='amount_currency']"):
                    elem = etree.Element(
                        'field', {
                            'name': 'amount_residual_currency',
                            'readonly': 'True'
                        })
                    orm.setup_modifiers(elem)
                    placeholder.addnext(elem)
            if not doc.xpath("//field[@name='amount_residual']"):
                for placeholder in doc.xpath(
                        "//field[@name='amount_currency']"):
                    elem = etree.Element(
                        'field', {
                            'name': 'amount_residual',
                            'readonly': 'True'
                        })
                    orm.setup_modifiers(elem)
                    placeholder.addnext(elem)
            # Remove credit and debit data - which is irrelevant in this case
            for elem in doc.xpath("//field[@name='debit']"):
                doc.remove(elem)
            for elem in doc.xpath("//field[@name='credit']"):
                doc.remove(elem)
            result['arch'] = etree.tostring(doc)
        return result
    

    About modifiers

    Modifiers are intended to replace attrs and other attributes (readonly, required, invisible). For the moment, they exist along side of those attributes. The reason to introduce them is to streamline things for the new web client so it can only look at one place. Also evaluation of modifiers will happen server-side, relinquishing the need for a python (-like) interpreter client-side. Finally, modifiers concrete syntax will be json (information taken from https://answers.launchpad.net/openobject-server/+question/168924).

    Conclusion

    In summary, answering your question, what you see in Edit form view is the pure XML code of the view, the same you will see in the XML files of the Odoo modules, whereas Fields view get is that code after being loaded and transformed to be rendered in client side.