Flask SQLAlchemy + Marshal_list_with +选定的列

时间:2018-09-08 20:44:40

标签: flask-sqlalchemy

我试图从联接两个表中拉出选定的列,并使用api.marshal_list_with返回结果。我的模型定义如下:

project_model = api.model('Project', {
'id': fields.String(readOnly=True, description='Unique project id'),
'name': fields.String(required=True, description='Project name'),
'created_user_id': fields.String(description='Id for user that created the project'),
'created_time': fields.String(readOnly=True, description='Time of first creation'),
'updated_time': fields.String(description='Time of last modification'),
'access_type': fields.String(description='Level of access')})

实际请求代码为:

@api.marshal_list_with(project_model)
def get(self):
    """
    Get details of all projects associated with user
    """
    user_id = get_jwt_identity()
    projects_l = db.session.query(Project, ProjectUserJunction).join(
        ProjectUserJunction).filter(ProjectUserJunction.user_id == user_id).values(
        Project.id, Project.name, Project.created_user_id,
        Project.created_time, Project.updated_time,
        ProjectUserJunction.access_type)
    return projects_l

当我进行get调用时,我得到的响应是一堆空值:

{
    "id": null,
    "name": null,
    "created_user_id": null,
    "created_time": null,
    "updated_time": null,
    "access_type": null
}

如果我在服务器上打印出project_l的内容,它不是空白,因此我知道查询没有中断。这可能只是我想念的封送信息。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我想将qs强制转换为列表,应该可以解决问题。