Dict' object has no attribute 'update_if_missing'

Hello everybody
Trying to insert into my custom doctype using python script opens json file then insert each record into database but i got this error

File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 219, in insert
self._set_defaults()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 627, in _set_defaults
d.update_if_missing(new_doc)
AttributeError: ‘dict’ object has no attribute ‘update_if_missing’

my code snippet:

    def execute():
    	with open(os.path.join(sys.path[0], "/home/frappe/frappe- 
            bench/apps/custom/custom_app/patches/mapping.json"), "r") as f:
    		data = json.load(f)
    		for x in data['feature_mapping']:
    			print (x['feature'])
    			doc=frappe.get_doc({
    				"doctype": "Mapping",
    				"feature":x['feature'],
    				"app_name":x["app_name"],
    				"roles":x["roles"]
    				})
    			if x['allow_totp']=='Yes':
    				doc.allow_totp=x["allow_totp"] 
    				doc.authenticator_roles=x["authenticator_roles"] 
    			doc.insert( ignore_permissions=True, # ignore write permissions during insert
    					    ignore_links=True, # ignore Link validation in the document
    					    ignore_if_duplicate=True, # dont insert if DuplicateEntryError is thrown
    					 
    					    )
    			frappe.db.commit()
    execute()

@Zeinab_Mohammed have you solved this. i am also facing same error

@abrarpv97 @Zeinab_Mohammed How have you solved this ?

I think what you want to do is insert an element in a table when you do :

  "roles":x["roles"]

Which it does not seems to be the good way to do it. Try to insert your elements like this :

doc=frappe.get_doc({
    "doctype": "Mapping",
    "feature":x['feature'],
    "app_name":x["app_name"],
})
doc.roles = []
doc.append("roles", {YOUR ROLE OBJECT})

Repeat this workflow for every table in your document (initialize => append).

Hope this will help.

1 Like

this solution is working thanks. but the problem i have faced was AttributeError: 'dict' object has no attribute 'is_new'. so