Вложенная схема Flask-Marshmallow со многими = true не работает - PullRequest
0 голосов
/ 21 октября 2019

Я пытаюсь сериализовать объект, содержащий коллекцию объектов, но коллекция не сериализуется правильно.

Документация говорит мне, что при работе с итеративными коллекциями объектов установите many = True.

Я сделал то же самое. Это мои схемы:

from marshmallow import fields, Schema

from app.institute.schema import InstituteSchema


class ResultRequestSchema(Schema):
    """Resultrequest"""

    class Meta:
        ordered = True

    resultRequestId = fields.Number(attribute="result_request_id")
    startYear = fields.Number(attribute="start_year")
    endYear = fields.Number(attribute="end_year")
    type = fields.String(attribute="type")
    institutes = fields.Nested(InstituteSchema, attribute="institutes", many=True)
from marshmallow import fields, Schema



class InstituteSchema(Schema):
    """Institute"""

    class Meta:
        ordered = True

    instituteId = fields.Number(attribute="institute_id")
    name = fields.String(attribute="name")
    brin = fields.String(attribute="brin")
    city = fields.String(attribute="city")

Мои модели при необходимости:

from sqlalchemy import Integer, Column, String
from sqlalchemy.orm import relationship
from app import db  # noqa
from app.result_request.model import result_request_institute_table

class Institute(db.Model):
    """Institute"""

    __tablename__ = "institute"

    institute_id = Column(Integer(), primary_key=True)
    brin = Column(String(255), nullable=False)
    name = Column(String(255), nullable=False)
    city = Column(String(255), nullable=False)
    resultrequests = relationship('ResultRequest', secondary=result_request_institute_table)
result_request_institute_table = Table('result_request_institute', db.metadata,
                                       Column('institute_id', Integer, ForeignKey('institute.institute_id')),
                                       Column('result_request_id', Integer,
                                              ForeignKey('result_request.result_request_id'))
                                       )


class ResultRequest(db.Model):
    """A ResultRequest"""

    __tablename__ = "result_request"
    result_request_id = Column(Integer(), primary_key=True)
    start_year = Column(Integer, nullable=False)
    end_year = Column(Integer, nullable=False)
    type = Column(Enum("TOPIC", "SENTIMENT", "UNKNOWN", name="type"), default="UNKNOWN")
    institutes = relationship('Institute', secondary=result_request_institute_table)

    result = relationship('Result')

Объект resultrequest имеет коллекцию объектов института. В документации Swagger поле представлено не как коллекция, а только как отдельный объект.


{
  "resultRequestId": 0,
  "startYear": 0,
  "endYear": 0,
  "type": "string",
  "institutes": {
    "instituteId": 0,
    "name": "string",
    "brin": "string",
    "city": "string"
  }
}

должно быть


{
  "resultRequestId": 0,
  "startYear": 0,
  "endYear": 0,
  "type": "string",
  "institutes": [{
    "instituteId": 0,
    "name": "string",
    "brin": "string",
    "city": "string"
  }]
}

...