Adding default user roles, modules and user permission

In frappe/core/doctype/user/

def validate(self):
        self.in_insert = self.get("__islocal")

def on_update(self):
        # clear new password
        if self.in_insert:
            default_role = {"Sales User","Purchase User","Stock User"}
            default_block_module = {"Dashboard","Support","POS","HR","Core",
                        "Manufacturing","Setup","Accounts","File Manager"
            for d_role in default_role:
                self.append("user_roles", {"role": d_role})
            for b_module in default_block_module:
                self.append("block_modules", {"module": b_module})
            frappe.permissions.add_user_permission("Division", self.division,, with_message=True)
            frappe.permissions.add_user_permission("Cost Center", self.cost_center,, with_message=True)
            frappe.msgprint("Please Save once more")

Using the above code,

  • adding user default permission is working ( once the user is created permissions are appended in tabletabDefaultValue)
  • adding user roles work some time, some time it doesnot work.( once the user is created user_roles are appended in table tab tabUserRole
  • adding block_modules (once the user is created block_modules are not getting appended in table tab tabBlock Module

If we save the user once again right after creating the user, adding default permission, roles and modules are working fine.

Please suggest what am I missing in the above code.I want to add module in the block_modules(tabBlock Module) under the user doctype if the document is local .

on_update event is called after saving, to set properties you must call your method from validate or before_save event