I have a model, in this model: field "categorie" can be related to many item. I need to set values of field2 if field "categorie" changed, item is filtered, but i need to keep items filtered in field2.
Here is python code:
class class4(models.Model):
_inherit = 'sale.order.line'
categorie = fields.Many2one('module.categorie')
item = fields.Many2one('module.item')
# field1 = fields.Char(compute='change_domain')
items = fields.Char(compute='change_domain')
field2 = fields.Many2many('module.item')
@api.one
@api.onchange('categorie')
def change_domain(self):
print(" »»» call change_domain")
res = {}
list = []
listChar = []
records = self.env['module.categorieitem'].search([('name', '=', self.categorie.id)])
for record in records:
list.append(record.item.id)
print('»»» set items value')
for record in self:
record.items = list
print("items list: ",record.items)
res['domain'] = {'item': [('id', 'in', list)]}
return res
Here is xml code:
<record id="sale_order_line_form_inherited" model="ir.ui.view">
<field name="name">sale_order.line.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//tree//field[@name='name']" position="after">
<field name="categorie"/>
<!--<field name="item" domain="[('name','in',items)]" />-->
<field name="item" domain="[]"/>
<field name="items"/>
<!--<field name="field2"-->
<!--widget="many2many_tags"-->
<!--options="{'no_create_edit': True}"/>-->
<field name="field2"
widget="many2many_tags"
options="{'no_create_edit': True}"/>
</xpath>
</field>
</record>