Add a Child Table Link Field in a Doctype via Connection - ashish-greycube/help GitHub Wiki

migrate.py

def after_migrate():
     update_dashboard_link_for_core_doctype(doctype='TAS Purchase Order',
                                            link_doctype='Quality Control QI',
                                            link_fieldname='tas_po', 
                                            parent_doctype='Quality Control QI',
                                            table_fieldname='pallet_details')

def update_dashboard_link_for_core_doctype(doctype,link_doctype,link_fieldname, parent_doctype=None, table_fieldname=None,group=None):
     print(doctype,link_doctype,link_fieldname,group)
     try:
         d = frappe.get_doc("Customize Form")
         if doctype:
             d.doc_type = doctype
         d.run_method("fetch_to_customize")
         for link in d.get('links'):
             if link.link_doctype==link_doctype and link.link_fieldname==link_fieldname:
                 # found so just return
                 return
         d.append('links', dict(link_doctype=link_doctype, 
                                link_fieldname=link_fieldname,
                                parent_doctype=parent_doctype, 
                                table_fieldname=table_fieldname,
                                group=group))
         d.run_method("save_customization")
         frappe.clear_cache()
     except Exception:
         frappe.log_error(frappe.get_traceback())

hook.py

after_migrate = "quality_inspection.migrate.after_migrate"

Ref : https://github.com/ashish-greycube/quality_inspection/blob/main/quality_inspection/migrate.py#L95