Dynamic form elements using attrs

So far, we have only looked into changing forms depending on the user's groups (the groups attribute on elements and the groups_id field on inherited views), but nothing more. This recipe will show you how to change forms based on the content of some fields in it.

How to do it...

  1. Define an attribute attrs on some form element:
    <field name="parent_id"
           attrs="{'invisible': [('is_company', '=', True)],
           'required': [('is_company', '=', False)]}" />
  2. Take care that all the fields you refer to are available on your form:
    <field name="is_company" invisible="True" />

This will make the field parent_id invisible if the partner is a company, and required if it's not a company.

How it works...

The attrs attribute contains a dictionary with the keys invisible, required, and readonly (all of them optional). The values are domains that may refer to the fields existing on the form (and really only those, so no dotted paths), and the whole dictionary is evaluated according to the rules for client side Python described earlier. So for example, you can access the context in the right hand operand.

There's more...

While this mechanism is quite straightforward for the scalar fields, it's less obvious how to handle the one2many and many2many fields. In fact, in standard Odoo you can't do much with those fields within an attrs attribute. But if you only need to check if such a field is empty or not, use [[6, False, []]] as your right hand operand.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.168.46