如何从数据库中的树数据结构创建一个json对象? [英] how to create a json object from tree data structure in database?
问题描述
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)
我想从这个模型创建一个json对象,用于导航菜单。
我想递归地解析它并构建一个分层结构JSON对象看起来像这样:
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}
]}
]
我使用名为 Flask-Restless 来查询数据库并返回json。如果你不想集成这样的东西,你可以继承你的SQLAlchemy模型,并运行to_json()方法就可以了。 。
class NewsCategory(db.Model,JsonSerializer)
class JsonSerializer(object):
可以用来标记一个SQLAlchemy模型类的mixin,
实现了一个:func:`to_json`方法。:func:`to_json`方法使用
与自定义:class:`JSONEncoder`类相关联。默认情况下,
mixin将假定SQLAlchemy模型的所有属性都是可见的
在JSON输出中扩展这个类来定制哪些属性是
public,hidden或者被传递给JSON序列化之前被修改
__json_public__ = None
__json_hidden__ = None
__json_modifiers__ = None
get get_field_names(self):
for self in .__ mapper __。iterate_properties:
yield p.key
$ b $ def to_json(self):
field_names = self.get_field_names()
public = self .__ json_pub lic__或field_names
hidden = self .__ json_hidden__ or []
modifiers = self .__ json_modifiers__ or dict()
rv = dict()
public:
rv [key] = getattr(self,key)
for modifiers.items():
value = getattr(self,key)
rv [key] =修饰符(值,自我)
隐藏键:
rv.pop(key,None)
return rv
贷: Github Overholt项目(Flask-Security的作者)
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"}
]}
]
I use a library called Flask-Restless for querying the database and returning json. It's made to work with SQLAlchemy.
If you're not looking to integrate with something like this, you can subclass your SQLAlchemy model and just run to_json() method on it.
class NewsCategory(db.Model, JsonSerializer)
class JsonSerializer(object):
"""A mixin that can be used to mark a SQLAlchemy model class which
implements a :func:`to_json` method. The :func:`to_json` method is used
in conjuction with the custom :class:`JSONEncoder` class. By default this
mixin will assume all properties of the SQLAlchemy model are to be visible
in the JSON output. Extend this class to customize which properties are
public, hidden or modified before being being passed to the JSON serializer.
"""
__json_public__ = None
__json_hidden__ = None
__json_modifiers__ = None
def get_field_names(self):
for p in self.__mapper__.iterate_properties:
yield p.key
def to_json(self):
field_names = self.get_field_names()
public = self.__json_public__ or field_names
hidden = self.__json_hidden__ or []
modifiers = self.__json_modifiers__ or dict()
rv = dict()
for key in public:
rv[key] = getattr(self, key)
for key, modifier in modifiers.items():
value = getattr(self, key)
rv[key] = modifier(value, self)
for key in hidden:
rv.pop(key, None)
return rv
Credit: Github Overholt project (author of Flask-Security)
这篇关于如何从数据库中的树数据结构创建一个json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!