Help

0

How can I filter a field inside a many2one_list based on the value of a many2many tag outside the many2one_list widget

Avatar
Jesse Rye

I am trying to create a domain filter that filters the selectable attributes based on the value of a many2many tag field outside of the many2one_list widget. I want the options the user can select in the attribute_id field to be limited based on the attributes in the attribute_value_ids field. But I get an error "Uncaught Error: NameError: name 'attribute_value_ids' is not defined" when trying to select a value in the attribute_id field inside the many2one_list widget.


Part of Current view code:

<sheet>
<group>
<field name="active" />
<field name="name" />
<field name="description" string="Description" widget="html" class="oe_edit_only" />
<field name="attribute_value_ids" string="Attributes" widget="many2many_tags" options="{'no_create': True}" />
</group>
<notebook>
<page string="Inventory Mapping">
<field name="webattribute_line_ids" widget="many2one_list" context="{'show_attribute': False}" nolabel="notrue">
<tree string="Variants" editable="bottom">
<field name="attribute_id" options="{'no_create': True}" domain="[('attribute_id','in',attribute_value_ids.attribute_id)]" />
<field name="value_ids" options="{'no_create': True}" domain="[('attribute_id', '=', attribute_id)]" context="{'default_attribute_id': attribute_id}" />
</tree>
</field>
</page>
</notebook>
</sheet>

Avatar
Discard
2 Answers
2
Avatar
Axel Mendoza
Best Answer

First of all, to refers to the field attribute_value_ids from the field attribute_id(of the tree definition for the field webattribute_line_ids) you should use parent.attribute_value_ids but fields of attribute_value_ids are not available, meaning that you cannot do parent.attribute_value_ids.attribute_id.

For that you need to build your domain in python re-implementing search or name_search method, for that I suggest that you need to pass some values in the context of the field attribute_id in the tree. For that you need to begin to pass the contexts values from the main form. Something like this seudo-code:

<field name="webattribute_line_ids" widget="many2one_list" context="{'show_attribute': False, 'attribute_value_ids': attribute_value_ids}" nolabel="notrue">
<field name="attribute_id" options="{'no_create': True}" context="{'attribute_value_ids': attribute_value_ids, 'special_search': True}" />
</field>

Using those values in the context you could implement your business rules for filtering those fields based on the others by re-implementing you search or name_search method.

*** Example ***

@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
if self.env.context.get('split_name', False):
name = False
for item in args:
if item[0] == 'name':
name = item
break
if name:
args.remove(name)
name = item[2]
domain = split_name_domain(name)
if domain:
args += domain
if self.env.context.get('current_user', False):
group_id = self.env['ir.model.data'].xmlid_to_res_id('medical_install.group_medical_doctor')
group_user = self.env['res.groups'].search([('id', '=', group_id), ('users.id', '=', self.env.uid)])
if group_user:
args.extend([('partner_id', '=', self.env.user.partner_id.id)])
return super(medical_physician, self).search(args, offset=offset, limit=limit, order=order, count=count)



Avatar
Discard
0
Avatar
Alexander Starostin
Best Answer

Axel, your solution sounds good, but the details are still unclear. 
Does re-implemented search of the model should refer to attribute_value_ids? Is it possible to find an example of implementation somewhere in custom addons?
Have things changed in ORM 10?

3 Comments
Avatar
Discard
Avatar
Axel Mendoza
-

No changes for v10 and no expected changes in the future, you would be able to use that way.

For an example I have updated my answer providing one

Cheers

Avatar
Alexander Starostin
-

Thanks a lot!

Avatar
Axel Mendoza
-

Thanks me with an upvote