6

I want to add child tables dynamically depending on records in another doctype.

Sajid Ijaz
  • 172
  • 1
  • 1
  • 8

4 Answers4

6

There are several method to add child in parent doc:

Method 1:

`

import frappe
parent = frappe.get_doc('Sales Order', 'SO-00002')
child = frappe.new_doc("Sales Order Item")
child.update({
    'company': 'company_name',
    'item_code': 'item_code',
    'item_name': 'item_name',
    'field': 'field_value'
    'parent': parent.name,
    'parenttype': 'Sales Order',
    'parentfield': 'items'
})
parent.items.append(child)

Method 2:

import frappe

parent = frappe.get_doc('Sales Order', 'SO-00002')
child = frappe._dict({
     'company': 'company_name',
    'item_code': 'item_code',
    'item_name': 'item_name',
    'field': 'field_value'
})
parent.items.append(child)

`

Nav
  • 61
  • 6
4

@Sajid liaz,

You can add the row in the child table using the append method

e.g.

doc = frappe.get_doc('Sales Order', 'SO-00002')
doc.append('items', {
    'company': 'company_name',
    'item_code': 'item_code',
    'item_name': 'item_name',
    'field': 'field_value'
})
doc.save()

where items is the child table's fieldname.

  • I want to add child tables dynamically not the data. for example if appraisal template has a child table with three rows then in appraisal it should generate three tables dynamically – Sajid Ijaz Apr 12 '19 at 10:00
  • 1
    @Sajid Ijaz You can not add the fields runtime, instead, add the HTML field and render the html table with the data – Makarand Bauskar Apr 12 '19 at 10:37
2

This is possible since DocTypes are treated as data in Frappe Framework. However, dynamic fields added at runtime must be added as Custom Fields.

from frappe.custom.doctype.custom_field.custom_field import create_custom_field

create_custom_field('Task' {
    "fieldname": 'values',
    "label": 'Values',
    "fieldtype": 'Table',
    "options": 'Child Table'
})
Faris Ansari
  • 285
  • 1
  • 5
1
parent = frappe.get_doc('Sales Order', 'SO-00002')
parent.append("items", {
    'company': 'company_name',
    'item_code': 'item_code',
    'item_name': 'item_name'
})
parent.save()
frappe.db.commit()
Abdo-Host
  • 2,470
  • 34
  • 33