Odoo Help

0

0
6 Answers
0
Samo Arko Slovenia
3/22/18, 9:05 AM

this key error happens when you try to use parent or child values in api.depends or api.onchange, because you can't change related models in real time, but only fields from the current model. The value will change only when you'll save. I made a mine to work so that I overwritten my write and create methods and in them I get the values from the related models and compute the fields that way. 

When working with onchange/depends think of it as working on a object and not the data in the DB. It's a bit weird that the id is a new id object but, this is just how it works. You can't even go trough ids... "for child in self.child_ids:"

@api.model
def create(self, vals):
    record = super(ModelName, self).create(vals)
    self.refresh_values()
    return record

  I'll get all the values and compute them in the self.refresh_values() method. In the method you can search for records with the code from the previous post or if you have them related its easier to go through relations. 

0
Avatar

Niyas Raphy India

--Niyas Raphy--
26373
| 7 7 11
Wayanad, Kerala, India
--Niyas Raphy--

Odoo Techno Functionalist

Feel free to connect :  niyasraphyk@gmail.com

Twitter:https://twitter.com/niyasraphy
LinkedIn: https://www.linkedin.com/in/niyasraphy/
FB:https://www.facebook.com/niyasraphy123
Whatsapp : +917736136763
Happy to help :)

Niyas Raphy India
3/22/18, 3:13 AM

Hi,


Change this line like this,

cost_rec = rec.env['cost.details'].search([('cost_id', '=', rec.id)])


Thanks

4 Comments
David Yao Hong Kong
3/22/18, 3:47 AM

even it does't showing error message, the calculation is not working, the value/result becomes 0. BTW, I tried doing this --- search([('cost_id', '=', 20)]) --- and it works fine (20 is what I find in database, it representing the id of that record)

David Yao Hong Kong
3/22/18, 4:38 AM

Hi, I tried to use search([('expense_id', '=', 'rec.cost_line.cost_id')]), then it gives me following error message.

This is log information:

Traceback (most recent call last):

File "C:\Users\...\odoo\odoo\fields.py", line 936, in __get__

value = record.env.cache.get(record, self)

File "C:\Users\...\odoo\odoo\api.py", line 960, in get

value = self._data[field][record.id][key]

KeyError: <odoo.api.Environment object at 0x0000000009E30908>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "C:\Users\...\odoo\odoo\http.py", line 647, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "C:\Users\...\odoo\odoo\http.py", line 307, in _handle_exception

raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])

File "C:\Users\...\odoo\odoo\tools\pycompat.py", line 87, in reraise

raise value

File "C:\Users\...\odoo\odoo\http.py", line 689, in dispatch

result = self._call_function(**self.params)

File "C:\Users\...\odoo\odoo\http.py", line 339, in _call_function

return checked_call(self.db, *args, **kwargs)

File "C:\Users\...\odoo\odoo\service\model.py", line 97, in wrapper

return f(dbname, *args, **kwargs)

File "C:\Users\...\odoo\odoo\http.py", line 332, in checked_call

result = self.endpoint(*a, **kw)

File "C:\Users\...\odoo\odoo\http.py", line 933, in __call__

return self.method(*args, **kw)

File "C:\Users\...\odoo\odoo\http.py", line 512, in response_wrap

response = f(*args, **kw)

File "C:\Users\...\odoo\addons\web\controllers\main.py", line 930, in call_kw

return self._call_kw(model, method, args, kwargs)

File "C:\Users\...\odoo\addons\web\controllers\main.py", line 922, in _call_kw

return call_kw(request.env[model], method, args, kwargs)

File "C:\Users\...\odoo\odoo\api.py", line 689, in call_kw

return call_kw_multi(method, model, args, kwargs)

File "C:\Users\...\odoo\odoo\api.py", line 680, in call_kw_multi

result = method(recs, *args, **kwargs)

File "C:\Users\...\odoo\odoo\models.py", line 5015, in onchange

record.mapped(dotname)

File "C:\Users\...\odoo\odoo\models.py", line 4429, in mapped

recs = recs._mapped_func(operator.itemgetter(name))

File "C:\Users\...\odoo\odoo\models.py", line 4408, in _mapped_func

vals = [func(rec) for rec in self]

File "C:\Users\...\odoo\odoo\models.py", line 4408, in <listcomp>

vals = [func(rec) for rec in self]

File "C:\Users\...\odoo\odoo\models.py", line 4675, in __getitem__

return self._fields[key].__get__(self, type(self))

File "C:\Users\...\odoo\odoo\fields.py", line 942, in __get__

self.determine_draft_value(record)

File "C:\Users\...\odoo\odoo\fields.py", line 1062, in determine_draft_value

self._compute_value(record)

File "C:\Users\...\odoo\odoo\fields.py", line 998, in _compute_value

getattr(records, self.compute)()

File "C:\Users\...\odoo\addons\hr_expense\models\hr_expense.py", line 94, in _compute_amount_my

print("show me", rec.expense_line.expense_id)

File "C:\Users\...\odoo\odoo\fields.py", line 934, in __get__

record.ensure_one()

File "C:\Users\...\odoo\odoo\models.py", line 4283, in ensure_one

raise ValueError("Expected singleton: %s" % self)

ValueError: Expected singleton: hr.expense.line(9, 10, 11, <odoo.models.NewId object at 0x000000000888C0D8>)

Niyas Raphy India
3/22/18, 4:52 AM

cost_rec = rec.env['cost.details'].search([('cost_id', '=', rec.id)])

David Yao Hong Kong
3/22/18, 5:01 AM

emmm....anything changed in your second code/answer?

Ask a Question
Writer
Keep Informed
1 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now