Flask SQLAlchemy选择子对象 - 返回JSONB列

时间:2016-04-26 14:56:28

标签: python flask flask-sqlalchemy flask-restful

我有一个有一对多关系的班级。我想在关系中归还所有父母的孩子;具体来说,我想返回子表中的所有JSONB对象。

这些是我的班级:

class ParentChild(Resource):
    def get(self, id):
           result = db.session.query(Parent).get(id)
           result_child = result.children
           return {'child': result_child}

然后使用Flask Restful,我试图选择所有这样的孩子:

raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <sqlalchemy.orm.dynamic.AppenderBaseQuery object at 0x106178da0> is not JSON serializable

有一个错误:

using ExcelDna.Integration;
using Excel = Microsoft.Office.Interop.Excel;

[ExcelFunction(Category = "Foo", Description = "Sets value of cell")]
public static Foo(String idx)
{
    Excel.Application app = (Excel.Application)ExcelDnaUtil.Application;
    Excel.Range range = app.ActiveCell;

    object[2,2] dummyData = new object[2, 2] {
        { "foo", "bar" },
        { 2500, 7500 }
    };

    var reference = new ExcelReference(
        range.Row, range.Row + 2 - 1, // from-to-Row
        range.Column - 1, range.Column + 2 - 1); // from-to-Column

    // Cells are written via this async task
    ExcelAsyncUtil.QueueAsMacro(() => { reference.SetValue(dummyData); });

    // Value displayed in the current cell. 
    // It still is a UDF and can be executed multiple times via F2, Return.
    return "=Foo()";
}

1 个答案:

答案 0 :(得分:1)

如果您想获得父母的每个孩子的所有data个对象。您可以执行以下查询:

result_child = db.session.query(Child.data).filter(Child.parentid == id).all()

如果您想使用children关系,可以迭代它:

result_child = [child.data for child in result.children]