gpt4 book ai didi

python - 来自邻接表的嵌套 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 11:00:15 25 4
gpt4 key购买 nike

我有一个 Flask RESTful API 应用程序,它具有以下 SQLAlchemy 类和一个代表邻接列表的自引用键:

class Medications(db.Model):
__tablename__ = 'medications'
id = Column(Integer, primary_key=True)
type = Column(String(64))
name = Column(String(64))
parent_id = Column(Integer, ForeignKey('medications.id'))
children = relationship("Medications")

我想要从 Medications 类返回的嵌套 JSON,按照

"objects": [
{
"children": [
{
"id": 4,
"name": "Child1",
"parent_id": 3,
"type": "Leaf"
},
{
"id": 5,
"name": "Child2",
"parent_id": 3,
"type": "Leaf"
}
],
"id": 3,
"name": "CardioTest",
"parent_id": null,
"type": "Parent"
}
],

根据 how-to-create-a-json-object-from-tree-data-structure-in-database我创建了序列化程序类

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

并将其子类化为我的 Medications 类,根据 class Medications(db.Model, JsonSerializer):

然后我调用 Models.to_json() 来获取我的序列化 JSON 输出,但是,唉,对象是空的:{'parent_id': None, 'type': None, ' children': [], 'name': None, 'id': None

但是,作为测试,如果我创建一个 Flask ReSTLess 端点,按照

manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Medications, methods=['GET'])

我得到以下输出:

"objects": [
{
"children": [
{
"id": 4,
"name": "Child1",
"parent_id": 3,
"type": "Leaf"
},
{
"id": 5,
"name": "Child2",
"parent_id": 3,
"type": "Leaf"
}
],
"id": 3,
"name": "CardioTest",
"parent_id": null,
"type": "Parent"
},
{
"children": [],
"id": 4,
"name": "Child1",
"parent_id": 3,
"type": "Leaf"
},
{
"children": [],
"id": 5,
"name": "Child2",
"parent_id": 3,
"type": "Leaf"
}
],

以及一些分页信息。

很好奇为什么我从使用 JsonSerializer 类的方法中得到一个空字典。我会使用 Flask ReSTLess 方法,但由于我将 Flask 用作 wsgi 应用程序,它会搞砸我的端点,此外,输出中不需要带有 children: [] 的节点。

最佳答案

我的问题的解决方案最终是使用 Marshmallow 和嵌套模式(在这篇文章 creating-a-tree-from-self-referential-tables-in-sqlalchemy 的帮助下,ala

# create SQLAlchemy object
record = db.session.query(Medications). \
options(joinedload_all("children", "children",
"children", "children",
"children", "children")).first()


class TestSchema(Schema):
name = fields.Str()
type = fields.Str()
id = fields.Int(dump_only=True)
parent_id = fields.Int(dump_only=True)
children = fields.Nested('self', many=True)

schema = TestSchema()

result = schema.dump(record)

工作起来非常棒,无需实现递归方法来构建树。

关于python - 来自邻接表的嵌套 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com