Fetch child table values using Jinja tags
Fetch child table values using Jinja tags
To reference any field on any DocType in ERPNext, utilize jinja templating. Just invoking doc.field name on a print format, where 'doc.name' is the variable name for a specific field, will accomplish this.
This method does not, however, apply to child tables that are part of a DocType. This article will show you how to navigate through and display every row related to a child table within any DocType.
Pre Requisites
For the related DocType, we would need the variable name of the child table. For the needed DocType, this is seen under the 'Customize Form' area. This is an illustration of the same.
In addition, we need the variable names for each field that must be referenced within the child table. This can be found in the associated child table's "Customize Form" section, as seen below.
Method 1. Displaying rows of a Child Table on an unordered list
{% for row in doc.items %} * Item Code: {{ row.get_formatted("item_code", doc) }} Quantity: {{ row.get_formatted("qty", doc) }} Rate: {{ row.get_formatted("rate", doc) }} Amount: {{ row.get_formatted("amount", doc) }} {% endfor %}
The output on a print format would be as follows
Method 2. Displaying rows of a Child Table as a table
{% for item in doc.items %} {% endfor %} Item Code Quantity Rate Amount {{item.item_code }} {{item.qty}} {{item.rate}} {{item.amount}}
The output on a print format would be as follows
This model can be used as a guide. By changing the Jinja template, any extra fields on the child table field can be fetched in a similar way.