Reputation: 3897
consider this method:
@api.multi
def _get_loc_req(self): #, cr, uid, context=None
"""Get if a field is required or not by a Localization
@param uid: Integer value of the user
"""
context = dict(self._context or {})
res = True
ru_brw = self.pool.get('res.users').browse(
self.cr, uid, uid, context=context)
rc_obj = self.pool.get('res.company')
rc_brw = rc_obj.browse(self.cr, uid, ru_brw.company_id.id, context=context)
if rc_brw.country_id and rc_brw.country_id.code == 'VE' and \
rc_brw.printer_fiscal:
res = False
return res
As You can see, I've added @api.multi
commented cr, uid, context=None
since multi should take care of these arguments automatically, right?
But for example, if I run this code, it throws this error:
Traceback (most recent call last):
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 862, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/web/controllers/main.py", line 854, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 679, in call_kw
return call_kw_model(method, model, args, kwargs)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/api.py", line 664, in call_kw_model
result = method(recs, *args, **kwargs)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/account/models/account_invoice.py", line 342, in create
invoice = super(AccountInvoice, self.with_context(mail_create_nolog=True)).create(vals)
File "/home/kristian/odoov10/odoo-10.0rc1c-20161005/odoo/addons/mail/models/mail_thread.py", line 227, in create
thread = super(MailThread, self).create(values)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 3778, in create
vals = self._add_missing_default_values(vals)
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 1618, in _add_missing_default_values
defaults = self.default_get(list(missing_defaults))
File "/home/kristian/.virtualenvs/odoov10/lib/python2.7/site-packages/odoo-10.0rc1c_20161005-py2.7.egg/odoo/models.py", line 1101, in default_get
defaults[name] = field.default(self)
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/model/invoice.py", line 61, in <lambda>
default=lambda s: s._get_loc_req(),
File "/home/kristian/odoov10/gilda/l10n_ve_fiscal_requirements/model/invoice.py", line 127, in _get_loc_req
self.cr, uid, uid, context=context)
AttributeError: 'account.invoice' object has no attribute 'cr'
On the method above, I've changed cr
to self.cr
as per new api, but no success, same error, and, for example self.pool.get
shouldn't it be self.env
? Or can it remain like that?
Generally speaking, what should it look like to be on new API? besides decorator, and cr
statements?
Another doubt I have, for example, these kind of methods, can be declared like:
@api.multi
def my_method(self):
return
But if originally it had, cr, uid, ids, context
etc, and these arguments are actually INSIDE the method, (like in the method above) should they be replaced? How should it look like?
How can I solve this? And if You can explain these interrogants would be awesome too!
Upvotes: 0
Views: 179
Reputation: 2633
I think your method could be written as:
@api.model
def _get_loc_req(self):
company = self.env.user.company_id
if (company.country_id and company.country_id.code == 'VE' and
company.printer_fiscal):
return False
return True
Regarding your second question - in the new API, there is no need to explicitly pass around cr,
uid
, ids
and context - cr
(self.env.cr
), uid
(self.env.user
) and context
(self.env.context
) are passed to your method via the environment (self.env
) and the notion of ids
is replaced with record sets. Odoo 10 does not support the old API (pre version 8), so I would recommend spending some time for getting used to the new API: link, link, link, link. Also, you might take a look at the code of official Odoo addons for version 10.0 - the are all written using the new API.
Upvotes: 2