如何在python flask sqlalchemy棉花糖应用程序中将一个属性对象数组呈现为平面数组?

时间:2019-02-25 03:58:17

标签: python flask sqlalchemy marshmallow

我是第一次编写Python Flask WebApp。使用Flask,SQLAlchemy,棉花糖作为我的主要软件包。我有一个嵌套模式,但是在父页面中我没有显示子代,但是我想将所有子代ID都带到父代中,以便在详细信息页面中可以加载所有子代。我将子级修剪为仅返回id,但是然后我不希望将它们作为一个属性对象,而是希望使用ids数组。

我该如何更改JSON,

{
  "description": "Report Name",
  "id": 1,
  "load_date_time": "2019-02-12T05:14:28+00:00",
  "children": [
    {
      "id": 1
    },
    {
      "id": 2
    },
    {
      "id": 3
    }
  ],
  "publish_date_time": "2018-09-03T00:00:00+00:00",
  "summary": "Summary will also be present. Usually two to three brief sentences about the content on the detail page."
}

{
  "description": "Report Name",
  "id": 1,
  "load_date_time": "2019-02-12T05:14:28+00:00",
  "children": [
    1,
    2,
    3
  ],
  "publish_date_time": "2018-09-03T00:00:00+00:00",
  "summary": "Summary will also be present. Usually two to three brief sentences about the content on the detail page."
}

棉花糖模式:

class ChildIdSchema(ma.Schema):
    class Meta:
        # Fields to expose
        fields = ('id', )
        ordered = True

class ParentSchema(ma.Schema):
    children = fields.Nested('ChildIdSchema', many=True)
    class Meta:
        # Fields to expose
        fields = ('id', 'description', 'children', 'summary', 'load_date_time', 'publish_date_time')
        ordered = True

2 个答案:

答案 0 :(得分:1)

如果您使用的是棉花糖3,则可以使用Pluck字段。

对于棉花糖2,将only参数用于Nested

# 2.x
class ParentSchema(ma.Schema):
    children = fields.Nested('ChildIdSchema', many=True, only='id')

# 3.x
class ParentSchema(ma.Schema):
    children = fields.Pluck('ChildIdSchema', 'id', many=True)

答案 1 :(得分:0)

您可以使用列表理解来完成此操作,如下所示:


#include <iostream>
#include <vector>
#include <math.h>

double getDerivitive(unsigned derivitive, double x, double cx, const std::function<double(double)> &f) {
    if (derivitive == 0) return f(x);

    double newCx = derivitive * cx;

    return (getDerivitive(derivitive - 1, x+newCx, cx, f) - getDerivitive(derivitive - 1, x-newCx, cx, f)) / (2*newCx);
}
std::vector<double> getCoefficient(unsigned precision, const std::function<double(double)> &f) {

    std::vector<double> coeffs(precision, 0.0);
    double iFactorial = 1;
    for (unsigned i = 0; i < precision; i++) {

        coeffs[i] = getDerivitive(i, 0.0, 0.01, f) / iFactorial;

        iFactorial *= (i+1);
    }

    return coeffs;
}

double getValueAt(double x, const std::vector<double> &coeffs) {

    double total = 0;

    double xToTheI = 1;
    for (unsigned i = 0; i < coeffs.size(); i++) {

        total += coeffs[i] * xToTheI;

        xToTheI *= x;
    }

    return total;
}

double getLimit(double x, const std::function<double(double)> &f) {

    std::function<double(double)> newFunc = [&](double atX) {
        return f(x + atX);
    };

    std::vector<double> coefficients = getCoefficient(10, newFunc);

    if (isnan(coefficients[0])) {
        coefficients[0] = newFunc(0.00001);

        if (abs(coefficients[0]) < 0.0001) {
            coefficients[0] = 0;
        }
    }

    for (unsigned i = 0; i < coefficients.size(); i++) {
        if (isnan(coefficients[i])) {
            coefficients[i] = 0;
        }
    }

    return getValueAt(0, coefficients);
}

int main(int argc, const char * argv[]) {


    std::function<double(double)> cosFTest = [](double x) {
        return cos(x);
    };

    std::cout << "Limit of cos(0)  ==  " << getLimit(0, cosFTest) << std::endl;

    std::function<double(double)> sinOverXTest = [](double x) {
        return sin(x)/x;
    };

    std::cout << "Limit of sin(x)/x  ==  " << getLimit(0, sinOverXTest) << std::endl;

    std::function<double(double)> polynomial = [](double x) {
        return (x*x) / x;
    };

    std::cout << "Limit of x^2 / x  ==  " << getLimit(0, polynomial) << std::endl;

    std::function<double(double)> funcTestInfinity = [](double x) {
        return 1.0/x;
    };

    std::cout << "Limit at 1/x apraoches infinity ==  " << getLimit(INFINITY, funcTestInfinity) << std::endl;

    return 0;
}