Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4017 Lượt xem

Hi

I'm having trouble calling the Create method in Python and it's throwing a NameError. The method that calls the Create method is called from JS.

Here is the JS (this works so maybe less important):

this._rpc({
model:'tcx.call.log',
method:'maakTest',
args:[
pId,
pName,
pNumber,
cDuration,
datum,
],
});

This is the Python code:

@api.one
def maakTest(partner_id, other_party_name, other_party_number, duration, call_date_time):
import pdb; pdb.set_trace()
vals = {'partner_id':partner_id,'other_party_name':other_party_name,'other_party_number':other_party_number,'duration':duration,'call_date_time':call_date_time}
self.create(vals)

Here is the error:


Traceback (most recent call last):
  File "/opt/odoo/odoo/odoo/http.py", line 654, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo/odoo/http.py", line 312, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/opt/odoo/odoo/odoo/tools/pycompat.py", line 87, in reraise
    raise value
  File "/opt/odoo/odoo/odoo/http.py", line 696, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo/odoo/odoo/http.py", line 344, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo/odoo/odoo/service/model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo/odoo/odoo/http.py", line 337, in checked_call
    result = self.endpoint(*a, **kw)
  File "/opt/odoo/odoo/odoo/http.py", line 939, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo/odoo/odoo/http.py", line 517, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/odoo/addons/web/controllers/main.py", line 962, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/opt/odoo/odoo/addons/web/controllers/main.py", line 954, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/opt/odoo/odoo/odoo/api.py", line 749, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "/opt/odoo/odoo/odoo/api.py", line 736, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-132>", line 2, in maakTest
  File "/opt/odoo/odoo/odoo/api.py", line 372, in loop
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/opt/odoo/odoo/odoo/api.py", line 372, in <listcomp>
    result = [method(rec, *args, **kwargs) for rec in self]
  File "/opt/odoo/odoo/custom_addons/threecx/models/call_log.py", line 27, in maakTest
    self.create(vals)
NameError: name 'self' is not defined

Now, I do know that I haven't defined 'self' in ths case, but I'm wondering on how to do it. Adding it to the parameter list in the method header and then adding null in JavaScript isn't working and I don't see any other way of calling the create method.

Any help is greatly appreciated!

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

You forgot to pass self in your method definition and also use "@api.model" instead of "@api.one"..

@api.model
def maakTes(self, partner_id, other_party_name, other_party_number, duration, call_date_time):


Ảnh đại diện
Huỷ bỏ
Tác giả

When I add self to the method definition I get another error saying I'm missing 1 required positional argument (call_date_time). How should I pass self as an argument in my JS?

Remove @api.one and use @api.model

Câu trả lời hay nhất

Hi,

The signature of your method misses the "self" argument.  Also use @api.model

Writing it like this should fix your problem:


    @api.model
def maakTest(self, partner_id, other_party_name, other_party_number, duration, call_date_time):
import pdb; pdb.set_trace()
vals = {'partner_id':partner_id,'other_party_name':other_party_name,'other_party_number':other_party_number,'duration':duration,'call_date_time':call_date_time}
self.create(vals)


Ảnh đại diện
Huỷ bỏ
Tác giả

When I add self to the method definition I get another error saying I'm missing 1 required positional argument (call_date_time). How should I pass self as an argument in my JS?

use @api.model - i edited my answer.

Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 7 20
8298
2
thg 6 19
7118
1
thg 12 18
12812
1
thg 6 25
3920
2
thg 11 22
6177