Odoo 8: Many2many domain filter

cgs picture cgs · Aug 21, 2015 · Viewed 16.8k times · Source

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...

Answer

Ludwik Trammer picture Ludwik Trammer · Aug 21, 2015

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)]"
    />