I have several organization types with a many2many relation describing which types that may be parent to other types (e.g. department can be parent to sub-department and working group). It's NOT a strict hierarchy (working group can be parent to other working groups), hence the many2many relation.
I have two fields on my organization_type object: allowed_parent_type_ids
and the inverse allowed_children_type_ids
.
Now I want to restrict the organization type field on my organization object depending on it's parent, so a child of a "department" can only select the organization types allowed to be children of departments and so on.
In my form view, I tried:
<field
name="organization_type_id"
domain="[('id', 'in', parent_id.organization_type_id.allowed_children_ids)]"
/>
I also tried to put a related field with allowed types on my organization object, but I always ends up with an error message. My last attempt was:
domain=[('id', 'in', allowed_type_ids)]
That gives an error message:
TypeError: not all arguments converted during string formatting
The client actually fetches a JSON object like "allowed_type_ids" = [0,1,2]
and if I replace allowed_type_ids in the domain expression with [0,1,2]
there are no errors and I get the three organization types in my selection...
Try this:
<field
name="organization_type_id"
domain="[('id', 'in', parent_id.organization_type_id.allowed_children_ids.ids)]"
/>
While allowed_children_ids
is a set of records, allowed_children_ids.ids
is a list of ids of those records.
You can also approach this from the other side. This should work and be event faster:
<field
name="organization_type_id"
domain="[('allowed_parent_type_ids', '=', parent_id.organization_type_id)]"
/>
EDIT: This trick doesn't work anymore in 9.0 and 10.0 even at the time I posted the message if your Odoo codebase was up to date see https://github.com/odoo/odoo/issues/16072 for more details.
For an alternative you can give a try to web_domain_field module. It is currently here: https://github.com/OCA/web/pull/567
Former answer:
To have a domain on a Many2many you will find a good answer from Olivier Dony on the Odoo FAQ: https://www.odoo.com/fr_FR/forum/aide-1/question/complex-many2many-domains-in-views-41777#answer_41784
In short, you need to adress the correct values as the Many2many value is a list of tuple like [(6, 0, ids)]
.
Thus you need to create a domain like this to compare ids:
domain=[('id', 'in', allowed_type_ids[0][2])]
Warning, this might not work on Odoo 9.0 in the case your many2many field is empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With