ERPNext.com Frappe Cloud Support Partners Foundation Frappe School

[Tutorial ] working with child table with python

hi
here is an easy way to work with child tables from backend

to remove all rows

to_remove = []
for d in doc.get("uoms"):
    #remove rows with condition ,  you can drop the if checking and append to the list right away to remove them all 
	if d.conversion_factor == 1 and d.uom != self.stock_uom:        
	   to_remove.append(d)

[doc.remove(d) for d in to_remove]

to add new row

    row = doc.append('uoms', {})
    row.uom = "SXM"
    row.conversion_factor = 12

for changing current row value

for d in doc.get("uoms"):
    if d.uom == "SXM":
        d.conversion_factor = 13
6 Likes

if there is another ways or additional hints please add it

@ahmadRagheb
if you want to remove all rows easy way is

def validate(self):
    self.uoms = []

If you want to get the rows filtered by a condition

def validate(self):
    for d in self.get('uoms', {'uom': 'SXM'}):
        d.conversion_factor = 13.0

Also to remove certain rows given a condition

def validate(self):
    [self.uoms.remove(d) for d in self.get('uoms') if d.conversion_factor == 1 and d.uom != d.stock_uom]
10 Likes

I am experiencing a very strange issue here with child tables Weird behavior while removing child record using self.remove in ERPNext: v12.9.3 (master)

Can anyone help please

Hey!!! I appreciate this post a lot, thank you @ahmadRagheb, @max_morais_dmm.

I have a scenario that I expect to complete with this help.

Where, I need to copy some fields of the new rows Submited in a Doc of a DocType Child table (DocType ACHT) of a Doc of a Doctype parent (DocType A), to a new rows of a DocType Child table (DocType BCHT) of an existent Doc in Doctype parent (DocType B), without erasing the existent rows of table BCHT.

I’m not sure if I did explain myself.

Abrazos

Hola @Ivan_Guevara como le va?

Can you create a dedicated post for you question?
I would like to answer it, but as another subject if you dont mind!

Gracias, y mucho gusto saludarte!