跳至内容
菜单
此问题已终结
2 回复
1497 查看

I try to get the Product template from selected Product Category. in my custom Model


prod_cate_ids=fields.Many2one('product.category', string="Product Category")

prod_templ=fields.Selection(selection='_get_prod_tem', string="Product Template", readonly=False)


@api.onchange('prod_cate_ids')def_get_prod_tem(self): 

​values=[]

​pro_tems=self.env['product.template'].search([('categ_id','=',self.prod_cate_ids.id)])

​if pro_tems is not None:

​for tem in pro_tems:

​values.append([tem.id,tem.name])

​return values


Error log:

Traceback (most recent call last):
  File "C:\odoo16\server\odoo\http.py", line 1584, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "C:\odoo16\server\odoo\service\model.py", line 133, in retrying
    result = func()
  File "C:\odoo16\server\odoo\http.py", line 1611, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "C:\odoo16\server\odoo\http.py", line 1815, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "C:\odoo16\server\odoo\addons\base\models\ir_http.py", line 154, in _dispatch
    result = endpoint(**request.params)
  File "C:\odoo16\server\odoo\http.py", line 697, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "C:\odoo16\server\odoo\addons\web\controllers\dataset.py", line 42, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "C:\odoo16\server\odoo\addons\web\controllers\dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\odoo16\server\odoo\api.py", line 461, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "C:\odoo16\server\odoo\api.py", line 448, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "C:\odoo16\server\odoo\models.py", line 6502, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "C:\odoo16\server\odoo\models.py", line 6214, in _onchange_eval
    process(method_res)
  File "C:\odoo16\server\odoo\models.py", line 6195, in process
    if res.get('value'):
AttributeError: 'list' object has no attribute 'get'

The above server error caused the following client error:
null


















形象
丢弃
最佳答案

Hi,

You can try dynamic domain.For that you need to change the selection field to many2one. 
Try this code:prod_cate_id=fields.Many2one('product.category', string="Product Category")

prod_templ_id=fields.Many2one('product.template', string="Product Template")

@api.onchange('prod_cate_ids')def _get_prod_tem(self):return {'domain': {'prod_templ_id': [('categ_id', '=', self.prod_cate_id.id)]}}

Hope it helps

形象
丢弃
最佳答案

Hi RATH SOKHOM,


There should be no return at the end of the onchange method.

You can store this "values" list in one field but can't return.


Regards,

Hemangi Rupareliya(rupareliyahemangi145@gmail.com)

形象
丢弃