Meaning of "object has no attribute" Error

Hi all,

I would ask about these error, which appears many time :
AttributeError:’ --------’ object has no attribute

My case is:
when i define a function in .py file and then call it in .js file, that error appear although the attribute he asked about is existed but he can not see it

any explanation plz??

Hi @MaysaaSafadi

Can you share the code

@rohit_w
Here the message:
Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 40, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 896, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 71, in runserverobj
frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
File “/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/run_method.py”, line 33, in runserverobj
fnargs, varargs, varkw, defaults = inspect.getargspec(getattr(doc, method))
AttributeError: ‘ServiceRequest’ object has no attribute ‘generate_auto_request_num’

Hi @MaysaaSafadi

Can you share your custom code?
It seems that, the py method is not called from the JS properly

Thank u for your reply @rohit_w

I called it and it work fine on my local but when i run it on server , the problem appaer
Here the function on .py file

def generate_auto_request_num (self):
d = datetime.date.today()
auto_num =self.generat_auto_num()
self.request_number = str(d.year) + str(d.month) +‘0000’+ str(auto_num)
self.auto_num = auto_num
return self.request_number

and in the .js file:
onload: function(frm) {
generate_auto_request_num(frm.doc)
},

@rohit_w here the rest of the code, sorry of that trubble,

var generate_auto_request_num = function (doc) {
** frappe.call({**
** doc: doc,**
** method: “generate_auto_request_num”,**
** callback: function ® {**
** // console.log®**
** refresh_many([“request_number”]);**
** }**
});
}

@MaysaaSafadi you need to whitelist the function:

@frappe.whitelist()
def generate_auto_request_num(self): 
...

in order to call from JS

@JoEz but the tow files are in the same place, the .py and the .js also

@MaysaaSafadi what u mean “are in the same place?”. In order to call a python method from js u need to whitelist

Hi @MaysaaSafadi

You need to pass the object(frm.doc) in the json

frappe.call({
  doc: frm.doc,
  method: generate_auto_request_num,
  callback: function(r, rt) {
     //call back operation
  }
})

for example see below code
JS Code

PY code

1 Like