Difference between frm.add_child and frappe.model.add_child

What is the difference between frm.add_child and frappe.model.add_child

		if (Array.isArray(data.items) && data.items.length > 0) {
            frm.clear_table("items"); // Clear previous table data
			valid_fields=frm.fields_dict.items.grid.fields_map
            data.items.forEach(item => {
                let row = frm.add_child("items"); // Add new row
				// let row = frappe.model.add_child(doc, "items");

                Object.keys(item).forEach(field => {
                    if (valid_fields[field]) {
                        row[field] = item[field]; // Only assign valid fields
                    }
                });
            });

        }
		frm.refresh_field("items"); // Refresh only once

Setting items dynamically using frm.add_child is making the UI laggy, is there a better alternative to fetch items from a doctype stored in the db and set it directly to a child table

The difference between frm.add_child and frappe.model.add_child in Frappe is frm.add_child is essentially a wrapper around frappe.model.add_child, with some additional functionality based on frm

1. frm.add_child

  • Defined in form.js, which means it is a method of frm (Form Object).
  • It operates on the frm object, which represents the current document form in the UI.
  • Automatically determines the child table’s doctype from fieldname.
  • Simpler and more intuitive for client-side scripting.

2. frappe.model.add_child

  • A lower-level function defined in Frappe’s frappe.model in create_new.js.
  • Works independently of frm and can be used even outside the form context.
  • Requires explicit parent document reference
  • Use `frappe.model.add_child when creating child records in scripts that operate outside the form.
2 Likes