我的 API 返回空白 json,而不是所需的嵌套架构

My API is returning blank json, instead of desired nested schema

我的架构是:

import time
from marshmallow import fields, Schema
from ramas_core.api.resources.base import CamelCaseSchema, base_filters, unfilterable_fields

class colorMapLayersSchema(CamelCaseSchema):
    #raster_layer
    id = fields.UUID()
    visibility = fields.Boolean()
    color_map_type = fields.String()
    #capture
    field_id = fields.UUID()
    event_date = fields.DateTime()
    event_date_time = fields.String()

class gridMapLayersSchema(CamelCaseSchema):
    #raster_layer
    id = fields.UUID()
    visibility = fields.Boolean()
    metric_type = fields.String()
    #capture
    field_id = fields.UUID()
    event_date = fields.DateTime()
    event_date_time = fields.String()

class indexMapLayersSchema(CamelCaseSchema):
    #raster_layer
    id = fields.UUID()
    visibility = fields.Boolean()
    index_map_type = fields.String()
    #capture
    field_id = fields.UUID()
    event_date = fields.DateTime()
    event_date_time = fields.String()

class LayersSchema(CamelCaseSchema):
    color_map_layers = fields.List(fields.Nested(colorMapLayersSchema))
    grid_map_layers = fields.List(fields.Nested(gridMapLayersSchema))
    index_map_layers = fields.List(fields.Nested(indexMapLayersSchema))

filterable_fields = {
    "capture_id": fields.UUID()
}

filterable_fields.update(base_filters)
filterable_fields.update(unfilterable_fields)

layers_schema = LayersSchema()

我的API代码是:

import logging
import json

import flask
from flask_smorest import Blueprint
from datetime import datetime

from ramas_core.api.resources.v1.layers import layers_schema
from ramas_core.models.ramas import User
from ramas_core.models.raster_layer import RasterLayer
from ramas_core.models.capture import Capture

from sqlalchemy import func
from sqlalchemy.dialects.postgresql import JSON

logger = logging.getLogger(__name__)
blueprint = Blueprint("layers_v1", __name__)

@blueprint.route("", methods=["GET"])
@blueprint.response(layers_schema)
def get_layers():
    """Return current user details."""
    session = flask.g.session
    username = flask.g.identity.username
    
    color = session.query(
        RasterLayer.id,
        RasterLayer.tags['visibility'].label('visibility'),
        RasterLayer.tags['legacy_sub_layer_type'].label('color_map_type'),
        Capture.field_id,
        Capture.capture_datetime.label('event_date'), 
    ).join(Capture, RasterLayer.capture_id == Capture.id) \
    .filter(RasterLayer.tags['legacy_layer_type'].astext == 'color').all() 

    grid = session.query(
        RasterLayer.id,
        RasterLayer.tags['visibility'].label('visibility'),
        RasterLayer.tags['legacy_sub_layer_type'].label('metric_type'),
        Capture.field_id,
        Capture.capture_datetime.label('event_date'), 
    ).join(Capture, RasterLayer.capture_id == Capture.id) \
    .filter(RasterLayer.tags['legacy_layer_type'].astext == 'grid').all()

    index = session.query(
        RasterLayer.id,
        RasterLayer.tags['visibility'].label('visibility'),
        RasterLayer.tags['legacy_sub_layer_type'].label('index_map_type'),
        Capture.field_id,
        Capture.capture_datetime.label('event_date'), 
    ).join(Capture, RasterLayer.capture_id == Capture.id) \
    .filter(RasterLayer.tags['legacy_layer_type'].astext == 'index').all()
    
    color_map_layers = flask.jsonify(color)
    grid_map_layers = flask.jsonify(grid)
    index_map_layers = flask.jsonify(index)

    result = {"colorMapLayers": [color_map_layers], "gridMapLayers": [grid_map_layers], "indexMapLayers": [index_map_layers]}
 
    return result

我的预期回复是:

{
  "colorMapLayers": [
    {
      "colorMapType": "rgb",
      "eventDate": "string",
      "eventDateTime": "2020-07-28T14:33:58.464Z",
      "fieldId": 0,
      "id": "string",
      "visibility": true
    }
  ],
  "gridMapLayers": [
    {
      "eventDate": "string",
      "eventDateTime": "2020-07-28T14:33:58.464Z",
      "fieldId": 0,
      "id": "string",
      "metricType": "plant_count",
      "visibility": true
    }
  ],
  "indexMapLayers": [
    {
      "eventDate": "string",
      "eventDateTime": "2020-07-28T14:33:58.464Z",
      "fieldId": 0,
      "id": "string",
      "indexMapType": "ndvi",
      "visibility": true
    }
  ]
}

但我目前得到的响应是:空 json {}

知道我做错了什么吗??

我还想知道如何随时随地将日期时间转换为字符串?

这也是定义嵌套数组的正确方法吗json?我需要操纵查询吗?

基于 Marshmallow 的 dump_serialize methods 的源代码,它看起来像是通过遍历属性来解析模式,检查字典中的属性,以及忽略丢失的键。但是,在您的情况下,检查的键是基于 layers_schema.__dict__ - 那些可能是 snake_case,而您 return 的对象的键是 camelCase。难道是更改字典的键可以解决问题吗?所以

result = {"color_map_layers": [color_map_layers], "grid_map_layers": [grid_map_layers], "index_map_layers": [index_map_layers]}