Added an optional render argument to as_trees that if true causes it to render the rows before building the tree
This commit is contained in:
1 parent
d01add8f2b
commit
1ee8d3cb97
1 file changed
+15
-3
+15
-3
@@ -10961,13 +10961,25 @@ class Rows(object):
|
|||||||
return dict([(key(r),r) for r in rows])
|
return dict([(key(r),r) for r in rows])
|
||||||
|
|
||||||
|
|
||||||
def as_trees(self, parent_name='parent_id', children_name='children'):
|
def as_trees(self, parent_name='parent_id', children_name='children', render=False):
|
||||||
|
"""
|
||||||
|
returns the data as list of trees.
|
||||||
|
|
||||||
|
:param parent_name: the name of the field to holding the reference to
|
||||||
|
the parent (default parent_id).
|
||||||
|
:param children_name: the name where the children of each row will be
|
||||||
|
stored as a list (default children).
|
||||||
|
:param render: whether we will render the fields using their represent
|
||||||
|
(default False) can be a list of fields to render or
|
||||||
|
True to render all.
|
||||||
|
"""
|
||||||
roots = []
|
roots = []
|
||||||
drows = {}
|
drows = {}
|
||||||
for row in self:
|
rows = list(self.render(fields=None if render is True else render)) if render else self
|
||||||
|
for row in rows:
|
||||||
drows[row.id] = row
|
drows[row.id] = row
|
||||||
row[children_name] = []
|
row[children_name] = []
|
||||||
for row in self:
|
for row in rows:
|
||||||
parent = row[parent_name]
|
parent = row[parent_name]
|
||||||
if parent is None:
|
if parent is None:
|
||||||
roots.append(row)
|
roots.append(row)
|
||||||
|
|||||||
Reference in new issue
Block a user