1👍
Via a query, not really. It’s just a fundamental limitation of relational databases that queries produce data in flat table structures, not nested data.
Some database engines can be made to output XML or json with a nested structure, but that’s usually through the use of subqueries, and is an advanced technique that isn’t remotely accessible to the ORM. It’d also be more work than iterating in Python.
Source:stackexchange.com