How to frappe.get_all() with child table fields? - erpnext

I have a "Parcel" Doctype
Which have a “Parcel Content” Doctype as a child table.
If i do:
frappe.get_doc('Parcel', 'NAME')
I get the object with the child table in it, it works for a single object.
But i need to get multiple objects with his child table fields.
So i can't render a custom jinja template.
eg:
frappe.get_all('Parcel', fields=['fields_of_parent', 'content'], filters=[])
So this will get me the filtered objects with the child table.

You have to query on the child table:
frappe.get_all("Parcel Content",
filters = dict(parent=parcel_name),
fields = [fields of child table])`

Use list comprehension:
[frappe.get_doc("Parcel", item.name).as_dict() for item in frappe.get_all("Parcel")]
This will return all doctypes with their respective child table items. Not even need to query based on child table name.

Related

Update parent object from child object

I have one parent object that is group and another child object that is individual. I have to concatenate two field individual name and designation of child object and update parent object field that is description using after trigger. I fail in this.
I tried but not work

Getting records through parentID from CategoryCollection in Typo3

I am using CategoryCollection to get the records of a specific category ID, but the problem is it only loads the exact category for e.g I have parent > child and I have attached child category ID to a record and I select child category, then it shows me the record fine, but if I select parentID, then it does not show the child category record.
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
$categoryID,
true,
'tx_myextension_table_name',
'categories'
);
Is there any built-in way to get the records of all child category if I select parent ID from CategoryCollectionor do I have to write something custom for that?
Unfortunately there is no built-in solution for complex selections like this. You will indeed need to write your own logic which could work like this:
Find categories whose parent is your category
Repeat this recursively for every category found until you don't find any children for each category anymore
Do a custom IN() query with the list of category UIDs
If you have deep category trees, the list of category UIDs could be put in a custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be tagged with sys_category. Alternatively you can add a sys_category_<uid> tag for every category UID in your list. This ensures that whenever something changes about one of the categories, the cache entries are dropped and you can rebuild the list.

Is it possible to have a subform that changes based on a indirectly related form?

I have a database structure in this format: Relationship Structure
I have worked out how to attach a subform which displays children of a particular record:
Ex 1
Ex 2
However, I can't seem to find a way of having a subform listing the related grandchildren of a record. For example, all formulations that reference a particular material.
Don't bind your subform directly to the table, but bind it to a query instead.
Handle the Child - Grandchild relation in the query, and the parent-child relation in the form, like you're already familiar with.

How to keep dirty data sorted in a Lightswitch Grid

I have an entity that has an "Order" column.
I have a grid onscreen bound to a collection of said entities.
Entity items should move up and down the grid, simply by changing the value of "Order" as the query that populates the collection is sorted by "Order".
The logic of this works perfectly, however I cannot get the grid to sort it's rows programmatically, neither does it do it automatically save for when the data initially loads.
Have you tried to write code in TestQuery_PreprocessQuery like:
partial void TestQuery_PreprocessQuery(ref IQueryable<Label> query)
{
query = query.OrderBy(c => c.Order);
}
Also there has been some considerations about calculated fields, relational fields and resorting. you can take a look at this Lightswitch grid sort problem

How do I get all rows from ITEM table, which are children of a parent ITEM table row, where relationship is stored separately?

How do I get all rows from ITEM table, which are children of a parent ITEM table row, where relationship is stored separately? How can I do a join to do this? "get all rows from ITEM table, which are children of this specific ITEM table row, all child items from this parent item, where relationship is stored in separate RELATIONSHIP table"
So given there is an ITEMS and a RELATIONSHIPS table. The key columns are:
ITEMS
* ID
* << other columns>>
RELATIONSHIPS
* PARENT_ID
* CHILD_ID
I'm trying to understand whether the DataSet / DataRelation approach could somehow map these relations. For example if I basically want a way to implement the request "Give me all children ITEMS in a DataRow[] form, given a parent ITEM DataRow, based on the RELATIONSHIPS table", is there a way to do this using a DataRelation? Of if not what would be the easiest way to do this using the DataSet approach?
EDIT: That is, assuming I am using a DataSet, and within the DataSet I have one DataTable for each of the physical database tables I described above.
Thanks
Top of my head, you're looking for roughly this solution (and I'm not entirely certain if I understand your datastructure correctly):
SELECT child.othercolumns
FROM items AS child, relationships AS r, items AS parent
WHERE r.parent_id=parent.id AND r.child_id=child.id