This question has been flagged
3 Replies
4866 Views

Hello,

How to get the first value of a one2many field if exist to a many2one field in the same model

Avatar
Discard
Best Answer

Hi Mohamed Ali 
I think the solution in this link 

https://www.odoo.yenthevg.com/default-value-many2one-in-odoo-8/

Try this

Avatar
Discard
Author Best Answer
Dear Jignesh Mehta

I tried this code

conjoint2 is one2many and conjoint is many2one

@api.one
@api.onchange('conjoint2')
def onchange_conjoint2(self):
self.conjoint = self.conjoint2[0].conjoint.id
return True

But i get this error

Odoo Server Error
Traceback (most recent call last):
  File "/opt/odoo/odoo/http.py", line 650, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo/http.py", line 310, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/opt/odoo/odoo/tools/pycompat.py", line 87, in reraise
    raise value
  File "/opt/odoo/odoo/http.py", line 692, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo/odoo/http.py", line 342, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo/odoo/service/model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo/odoo/http.py", line 335, in checked_call
    result = self.endpoint(*a, **kw)
  File "/opt/odoo/odoo/http.py", line 936, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo/odoo/http.py", line 515, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/addons/web/controllers/main.py", line 934, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/opt/odoo/addons/web/controllers/main.py", line 926, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/opt/odoo/odoo/api.py", line 689, in call_kw
    return call_kw_multi(method, model, args, kwargs)
  File "/opt/odoo/odoo/api.py", line 680, in call_kw_multi
    result = method(recs, *args, **kwargs)
  File "/opt/odoo/odoo/models.py", line 5099, in onchange
    record._onchange_eval(name, field_onchange[name], result)
  File "/opt/odoo/odoo/models.py", line 4994, in _onchange_eval
    method_res = method(self)
  File "<decorator-gen-68>", line 2, in onchange_conjoint2
  File "/opt/odoo/odoo/api.py", line 369, in loop
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/opt/odoo/odoo/api.py", line 369, in <listcomp>
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/home/mohamed/itc_package/itc_package/models/models.py", line 106, in onchange_conjoint2
    self.conjoint = self.conjoint2[0].conjoint.id
  File "/opt/odoo/odoo/models.py", line 4762, in __getitem__
    return self._browse((self._ids[key],), self.env)
IndexError: tuple index out of range
Avatar
Discard
Best Answer

Hello


Mohamed Ali Drira,

Try this :-

self.many2one_field = self.one2many_field[0].many2one_field.id


Note:- Many2one field in the One2many field has same object of Many2one field.


Hope it will helps you.

Thanks,



Avatar
Discard