def get_linked_docs(doctype, name, metadata_loaded=None, no_metadata=False): if not metadata_loaded: metadata_loaded = [] meta = frappe.desk.form.meta.get_meta(doctype) linkinfo = meta.get("__linked_with") results = {} if not linkinfo: return results me = frappe.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True) for dt, link in linkinfo.items(): link["doctype"] = dt link_meta_bundle = frappe.desk.form.load.get_meta_bundle(dt) linkmeta = link_meta_bundle[0] if not linkmeta.get("issingle"): fields = [d.fieldname for d in linkmeta.get("fields", {"in_list_view":1, "fieldtype": ["not in", ["Image", "HTML", "Button", "Table"]]})] \ + ["name", "modified", "docstatus"] fields = [ "`tab{dt}`.`{fn}`".format(dt=dt, fn=sf.strip()) for sf in fields if sf ] try: if link.get("get_parent"): if me and me.parent and me.parenttype == dt: ret = frappe.get_list( doctype=dt, fields=fields, filters=[[dt, "name", '=', me.parent]]) else: ret = None elif link.get("child_doctype"): ret = frappe.get_list(doctype=dt, fields=fields, filters=[[ link.get('child_doctype'), link.get("fieldname"), '=', name ]]) else: ret = frappe.get_list( doctype=dt, fields=fields, filters=[[dt, link.get("fieldname"), '=', name]]) except frappe.PermissionError: continue if ret: results[dt] = ret if not no_metadata and not dt in metadata_loaded: frappe.local.response.docs.extend(link_meta_bundle) return results
def get_linked_docs(doctype, name, metadata_loaded=None, no_metadata=False): if not metadata_loaded: metadata_loaded = [] meta = frappe.desk.form.meta.get_meta(doctype) linkinfo = meta.get("__linked_with") results = {} if not linkinfo: return results me = frappe.db.get_value(doctype, name, ["parenttype", "parent"], as_dict=True) for dt, link in linkinfo.items(): link["doctype"] = dt link_meta_bundle = frappe.desk.form.load.get_meta_bundle(dt) linkmeta = link_meta_bundle[0] if not linkmeta.get("issingle"): fields = [ d.fieldname for d in linkmeta.get( "fields", {"in_list_view": 1, "fieldtype": ["not in", ["Image", "HTML", "Button", "Table"]]} ) ] + ["name", "modified", "docstatus"] fields = ["`tab{dt}`.`{fn}`".format(dt=dt, fn=sf.strip()) for sf in fields if sf] try: if link.get("get_parent"): if me and me.parent and me.parenttype == dt: ret = frappe.get_list(doctype=dt, fields=fields, filters=[[dt, "name", "=", me.parent]]) else: ret = None elif link.get("child_doctype"): ret = frappe.get_list( doctype=dt, fields=fields, filters=[[link.get("child_doctype"), link.get("fieldname"), "=", name]], ) else: ret = frappe.get_list(doctype=dt, fields=fields, filters=[[dt, link.get("fieldname"), "=", name]]) except frappe.PermissionError: frappe.local.message_log.pop() continue if ret: results[dt] = ret if not no_metadata and not dt in metadata_loaded: frappe.local.response.docs.extend(link_meta_bundle) return results