How to troubleshoot AttributeError:X object has no attribute

Hi,

I have created a new doctype and added a child table named ‘item_papers’. It worked fine initially, but after adding another table and some more modifications, it started giving ‘AttributeError’. In order to troubleshoot, I tried to print all the member variables, and rightly I don’t see the table variable. I tried various methods including duplicating the doctypes, but none resolved it properly. Could someone let me know how this mapping(doctype to Object and DB Table) works and ways to troubleshoot associated issues?

Thanks
Sathish

@spoojary,

Can you share the error traceback?

11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/app.py”, line 55, in application
11:41:06 web.1 | response = frappe.handler.handle()
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/handler.py”, line 21, in handle
11:41:06 web.1 | data = execute_cmd(cmd)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
11:41:06 web.1 | return frappe.call(method, **frappe.form_dict)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/init.py”, line 907, in call
11:41:06 web.1 | return fn(*args, **newargs)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/handler.py”, line 80, in runserverobj
11:41:06 web.1 | frappe.desk.form.run_method.runserverobj(method, docs=docs, dt=dt, dn=dn, arg=arg, args=args)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/desk/form/run_method.py”, line 35, in runserverobj
11:41:06 web.1 | r = doc.run_method(method)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/model/document.py”, line 667, in run_method
11:41:06 web.1 | out = Document.hook(fn)(self, *args, **kwargs)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/model/document.py”, line 892, in composer
11:41:06 web.1 | return composed(self, method, *args, **kwargs)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/model/document.py”, line 875, in runner
11:41:06 web.1 | add_to_return_value(self, fn(self, *args, **kwargs))
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/frappe/frappe/model/document.py”, line 661, in
11:41:06 web.1 | fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)
11:41:06 web.1 | File “/home/spoojary/factory-bench/apps/corrugation/corrugation/corrugation/doctype/cm_box_description/cm_box_description.py”, line 20, in populate_raw_materals
11:41:06 web.1 | if len(self.item_papers) == 0:
11:41:06 web.1 | AttributeError: ‘CMBoxDescription’ object has no attribute ‘item_papers’

I thought it won’t be of much use as it is in a custom app. After doing some more experimentation, it looks like the child table attribute is getting added some time later in the object lifecycle. When I was getting the error, I was making the call from javascript onload() function. The document is still local at that stage, however I could see all the attributes except the child table from server side(python script). Then I moved the call from javascript onload() to start of python on_update(). During that time, the object has all the attributes including child table. So looks like the attributes are getting added somewhere in the middle. What I am really looking for is some information on this mapping(doctype to Object and DB Table) and troubleshooting tips.

Perhaps suggesting an answer to my problem might be easier than explaining the internal mappings.

The new doctype I have created has two child tables. I want to add some rows to those child tables automatically with partial information. That helps the user to fill only the remaining fields. So I tried doing that by calling a python function in java onload() method. That adds the rows which user can update later. However refreshing the page adds the same rows again from onload() function. To prevent that, I tried to check the child table length before adding the rows. That resulted in attributeError as I am trying access the child table which doesn’t exist yet!
I have fixed that by adding following check in js onload function.
if (!frm.doc.__islocal) return;
Just wondering if there is any better way of doing this.