I'm using flask with the following model:
class NewsCategory(db.Model):
__tablename__ = 'news_category'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64))
parent_id = db.Column(db.Integer, db.ForeignKey('news_category.id'))
children = db.relationship("NewsCategory")
And I want to create a json object from this model for use in navigation menu.
I would like to parse it recursively and build a hierarchical JSON object that looks something like this:
tree = [{"title": "Node 1", "id": "1"},
{"title": "Folder 2", "id": "2", "folder": "true", "children": [
{"title": "Node 2.1", "id": "3"},
{"title": "Node 2.2", "id": "4"}
]}
]