Try ERPNext Buy Support Partners Foundation

How to insert child table records link to existing parent table row

I’ve gone through many posts regarding frappe.get_doc() to insert child record. But i’m not sure how it works.

I have a parent doctype which contains a child doctype as a table grid.
I’ve developing a method to import child data to parent by columns (Horizontal), not row as the default Data Import tool (Vertical).

Ex: Employee | Base Salary | OT | Incentive | Allowance
EMP/001 | 500 | 50 | 70 | 900

Can anyone help to show how the dict data look like and how to insert data properly without using raw sql?

Thanks in advance.


You can use the doc.append("childtable_fieldname", {}) method it will append the empty row and return the row reference, Then you can just save the doc.


doc = frappe.get_doc("Sales Order")
row = doc.append("items", {})
row.item_code = "Test Item"
row.qty = 2

or you can also use the following format

doc = frappe.get_doc("Sales Order")
row = doc.append("items", {
	"item_code": "Test Item",
	"qty": 2

I have created new doctype(not custom) and also migrated: Monthly Salary Component Data
frappe.get_doc(“Monthly Salary Component Data”), it returns “Monthly Salary Component Data Not found”

“childtable_fieldname” is child primary key column?

This my child table columns.

Sorry for lots of question as I have less experience with Python as well as Frappe.

Thanks for you helpful response.

Parent table: Monthly Salary Component Data
child table: Salary Component Data Table

The screenshot shows that the child table doctype (Salary Component Data Table) atleast has a database table. Is Monthly Salary Component Data visible in the doctype list after creating it? Could you check if a similar backend table has been created for it through mysql?

Monthly Salary Component Data doctype would have a field for the child table doctype Salary Component Data Table. Replace “childtable_fieldname” by the name of that field in the code.

1 Like

yes, It’s in the doctype list. I also tried frappe.get_doc(“Sales Order”), it return Not Found as well. It works if I put another parameter frappe.get_doc(“Doctype”, “value of column name”)

Now i’m crystal clear about childtable_fieldname :slight_smile:

Here’s what I tried but still not success.
d = frappe._dict({“employee”:“EMP/001”,“idx”: 1, “salary_component”:“Incentive”,“total_amount”: 100})
d[“doctype”] = “Salary Component Data Table”
d[“status”] = 0
doc = frappe.get_doc(“Monthly Salary Component Data”, “June 2017”)
child = doc.append(“salary_component_data_table”, d)

I got two errors:
AttributeError: ‘NoneType’ object has no attribute ‘name’
TypeError: ‘Document’ object does not support indexing

My purpose: To have a doctype for importing data horizontal columns to override salary component in salary structure

frappe._dict object properties are not indexed as regular dicts, they can be set as:
d.doctype = "Salary Component Data Table"
d.status = 0

Would be helpful to know which line caused this error.

Here’s my scenario. I have a parent Doctype which has a child doctype as grid table.

In parent doctype, I have created a record name “June 2017” and some child record by UI. I want to add more child records via programming.

My Child doctype’s name is 8 digit string (unreadable), I don’t know how it’s generated.

I tried to created Child record:
doc = frappe.get_doc({dictionary with all field, except name})

It gives error AttributeError: ‘NoneType’ object has no attribute ‘name’

Finally, i’ve managed it works by changing how doc name generated, i set it as series_name and doc.flags.ignore_links = True, doc.flags.ignore_validate = True

how to child table last row remark and date set in same parent doctype field?
Using Custom Script.
Please any help.