我正在使用flask-marshmallow和marshmallow-sqlalchemy
我想拥有自己的HATEOAS实现:对于n对多关系,以及链接,我想拥有对象的数量
为此,我有一个定期的sqlalchemy模型,具有多对多的关系:
class ParentChild(Model):
__tablename__ = 'parrent_child'
parent_id =Column(Integer, ForeignKey('parent.id'), primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'), primary_key=True)
class Parent(Model):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String())
children = relationship('Child', secondary='parent_child', back_populates='parents')
class Child(Model):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String())
parents = relationship('Parent', secondary='parent_child', back_populates='children')
使用以下marshmallow架构,我设法获得我想要的数据:
class ParentSchema(Schema):
class Meta:
model = Parent
children = URLFor('api.parents_children_by_parent_id', parent_id='<id>')
children_count = base_fields.Function(lambda obj: len(obj.children))
返回:
{
"id" : 42,
"name" : "Bob",
"children" : "/api/parents/42/children",
"children_count" : 3
}
但是当我想要封装像这样的字段时,我遇到了问题:
{
"id": 42
"name": "bob",
"children": {
"link": "/api/parents/42/children",
"count": 3
}
}
我尝试使用base_fields.Dict
:
children = base_fields.Dict(
link = URLFor('api.parents_children_by_parent_id', parent_id='<id>'),
count = base_fields.Function(lambda obj: len(obj.children))
)
但我得到了TypeError: Object of type 'Child' is not JSON serializable
我尝试了各种其他解决方案,没有成功:flask-marshmallow的Hyperlinks
只接受超链接的字典,而不是函数。
我认为解决方案是使用base_fields.Nested
,但它打破了URLFor
无法捕捉到'<id>'
的行为。我在文档中找不到解决方案。
在某些时候,很难想出开箱即用。我错过了什么吗?任何帮助,将不胜感激。
所以我找到了一个我要发布的解决方法,但我认为它可以改进。
要用我想要的对象覆盖children
字段,我使用base_fields.Method
:
class ParentSchema(Schema):
class Meta:
model = Parent
children = base_fields.Method('build_children_obj')
def build_children_obj(self, obj):
return {
"count": len(obj.children),
"link": URLFor('api.parents_children_by_parent_id', parent_id=obj.id)
}
那时,我得到了TypeError: Object of type 'URLFor' is not JSON serializable
所以在检查了_serialize
的URLFor
方法的来源后,我在我的(自定义的)JSONEncoder中添加了一个检查:
if isinstance(o, URLFor):
return str(o._serialize(None, None, o))
我终于得到了我想要的有效载荷,但我发现它并不干净。有任何想法吗?
编辑:经过测试,我发现通过加载整个孩子列表,len(obj.children)
获得计数是非常昂贵的资源。相反,我做db.session.query(func.count(Children.id)).filter(Children.parents.any(id=obj.id)).scalar()
哪个更优化。