Reading and modifying values received by frappe.get_doc

Hello everyone

Let’s say, I’m executing a script that contains the following line (with a .csv as input)

doc = frappe.get_doc(
                    {
                        "doctype":"Item",
                        "item_code": item_code,
                        "item_name": item_name
                    } 
print(doc)

Then, when I print that statement, I would expect something like

["Item", "101", "Clock"]

Instead, I get this:

<erpnext.stock.doctype.item.item.Item object at 0x7f89bc9f2310>

Is there a way, to format this into an array, where I can also modify values, so “101” for example would become “G-10”? Or is it not meant to be read like this?

Kind regards,
Val

Hello Val !

What you got is an object of Class Item. This object has all the fields you see in the doctype and want to change. Change them like this :slight_smile:

item.has_serial_no = 1
item.item_name = "Mouse Pad"

And then you need to save it like this :

item.save()

Since item is a save only doctype you only need save. However there are other doctypes that are submittable. You can use the submit() method just as I used for save.

You can try this out in bench console.

In case if you don’t want an object but rather a list of values you can try for get_values function.

2 Likes

Hi root13F,

Thank your for your response. This is really helpful, but does not seem to work. “item” is not defined as such.

Edit: I’m an idiot, of course I have to pull it from “doc.” in my case. Thank you for your help.

1 Like

Here’s one tip for the traversing the child table:

assuming item_detail is a child doctype field in the Item… use a loop to go through the table

for row in item.get('item_detail'):
     print(row.id)
     print(row.item_name)
     .
     .

For adding rows to a child doctype, you can do it like this:

item.append('item_detail',{
    'fieldname':'fieldvalue',
    ''
})
item.save()
frappe.db.commit()

You’ll find more here : Developer Cheatsheet and even more on this forum