Reputation: 57
im trying to add more options to an existing selection field (em_y) depending on what's selected from another selection field (em_x) . here is my code
from odoo import models,fields,api
class HrEmployee(models.Model):
_inherit=['hr.employee']
em_x = fields.Selection(selection=[('x A','x A'),('x B','x B')],string='X')
em_y = fields.Selection([],string='Y')
@api.onchange('em_x')
def onchange_em_x(self):
if self.em_x == 'x A':
em_y = fields.Selection(selection_add = [('y A', 'y A'),('y B', 'y B')])
elif self.em_x == 'x B':
em_y = fields.Selection(selection= [('y C', 'y C'),('y D', 'y D')])
but this code is not working it display this
Traceback (most recent call last): File "/opt/odoo/odoo/http.py", line 643, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/opt/odoo/odoo/http.py", line 301, in _handle_exception raise exception.with_traceback(None) from new_cause ValueError: Wrong value for hr.employee.em_y: <odoo.fields.Selection>
can someone help me THanks.
Upvotes: 0
Views: 955
Reputation: 1
It isn't possible with "Selection" field. You must use "Many2one" field and in onchange method return domain.
class HrEmployee(models.Model):
_inherit=['hr.employee']
em_x = fields.Many2one('em.x',string='X')
em_y = fields.Many2one('em.x',string='Y',domain=[('id','=',0)])
@api.onchange('em_x')
def onchange_em_x(self):
emx_a = self.env.ref('module_name.emx_a').id
emx_b = self.env.ref('module_name.emx_b').id
if self.em_x.id == emx_a:
x_domain = [('id','in',[emx_a,emx_b])]
elif self.em_x.id == emx_b:
emx_c = self.env.ref('module_name.emx_c').id
emx_d = self.env.ref('module_name.emx_d').id
y_domain = [('id','in',[emx_c,emx_d])]
return {'domain': {'em_x': x_domain,'em_y': y_domain}}
class EmX(models.Model):
_name='em.x'
name = fields.Char(string='Name',required=True)
#data.xml
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="emx_a" model="em.x">
<field name="name">A</field>
</record>
<record id="emx_b" model="em.x">
<field name="name">B</field>
</record>
<record id="emx_c" model="em.x">
<field name="name">C</field>
</record>
<record id="emx_d" model="em.x">
<field name="name">D</field>
</record>
</data>
</odoo>
Upvotes: 0