After almost a year I decided to update my Python code inside Odoo 10. I've a custom module writing on a different model:
from odoo import api, fields, models
import openerp.addons.decimal_precision as dp
import logging
_logger = logging.getLogger(__name__)
class AccountDivina(models.Model):
_inherit = ['account.bank.statement.line']
taxes = fields.Float(
'Taxes', compute='_compute_taxes', store=True, digits=(5, 2), default='0.00')
@api.multi
@api.depends('journal_id')
def _compute_taxes(self):
ptx = self.env['pos.config'].search([('id', '=', 1)])
acj = self.env['account.journal']
if self.journal_id.id == acj.search([('code', '=', 'CR01')]).id:
self.taxes = ((self.amount / 100) * ptx.cr_tax)
elif self.journal_id.id == acj.search([('code', '=', 'DB01')]).id:
self.taxes = ((self.amount / 100) * ptx.db_tax)
pos_tax = self.env['pos.order'].search([('id', '=', self.pos_statement_id.id)])
new_val = pos_tax.taxes + self.taxes
print self.pos_statement_id.id
print pos_tax, pos_tax.taxes, new_val, pos_tax.margin
pos_tax.write({'taxes': new_val})
self.env.cr.commit()
I can search and browse with no issues other models, but suddenly the write function stop working, and I don't remember if and when I change the code.
The log:
2019-01-23 10:34:09,005 11925 ERROR divina_test6 odoo.http: Exception during JSON request handling.
Traceback (most recent call last):
File "/opt/odoo/odoo/http.py", line 641, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/opt/odoo/odoo/http.py", line 683, in dispatch
result = self._call_function(**self.params)
File "/opt/odoo/odoo/http.py", line 333, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/opt/odoo/odoo/service/model.py", line 101, in wrapper
return f(dbname, *args, **kwargs)
File "/opt/odoo/odoo/http.py", line 326, in checked_call
result = self.endpoint(*a, **kw)
File "/opt/odoo/odoo/http.py", line 941, in __call__
return self.method(*args, **kw)
File "/opt/odoo/odoo/http.py", line 506, in response_wrap
response = f(*args, **kw)
File "/opt/odoo/addons/web/controllers/main.py", line 892, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/opt/odoo/addons/web/controllers/main.py", line 884, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/opt/odoo/odoo/api.py", line 687, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/opt/odoo/odoo/api.py", line 672, in call_kw_model
result = method(recs, *args, **kwargs)
File "/opt/odoo/addons/point_of_sale/models/pos_order.py", line 609, in create_from_ui
pos_order = self._process_order(order)
File "/opt/odoo/addons/point_of_sale/models/pos_order.py", line 115, in _process_order
order.add_payment(self._payment_fields(payments[2]))
File "/opt/odoo/addons/point_of_sale/models/pos_order.py", line 818, in add_payment
self.env['account.bank.statement.line'].with_context(context).create(args)
File "/opt/odoo/addons/account/models/account_bank_statement.py", line 393, in create
line = super(AccountBankStatementLine, self).create(vals)
File "/opt/odoo/odoo/models.py", line 3847, in create
record = self.browse(self._create(old_vals))
File "/opt/odoo/odoo/models.py", line 4006, in _create
self.recompute()
File "/opt/odoo/odoo/models.py", line 5336, in recompute
vals = rec._convert_to_write({n: rec[n] for n in ns})
File "/opt/odoo/odoo/models.py", line 5336, in <dictcomp>
vals = rec._convert_to_write({n: rec[n] for n in ns})
File "/opt/odoo/odoo/models.py", line 5232, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/opt/odoo/odoo/fields.py", line 918, in __get__
value = record._cache[self]
File "/opt/odoo/odoo/models.py", line 5583, in __getitem__
value = self._recs.env.cache[field][self._recs.id]
Using some print statement in my code I can clearly see the fields/values I need are present, simply I can't update them on model.
For now I'm backing using the cursor
new_val = pos_tax.taxes + self.taxes
new_margin = pos_tax.margin - new_val
sql_wrt_cst = 'UPDATE pos_order SET (taxes, margin) = (%s, %s) WHERE id = %s'
self.env.cr.execute(sql_wrt_cst, [new_val, new_margin, self.pos_statement_id.id])
self.env.cr.commit()
But I would like to migrate to the new method.
Any clue for me?
F.