many=true的flaskmarshullow嵌套架构不工作

2024-06-01 04:53:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图序列化一个包含对象集合的对象,但该集合未正确序列化。在

文档告诉我,在处理iterable对象集合时,设置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)
^{pr2}$

我的模型(如果需要):

^{3}$
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对象有一组institute对象。在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"
  }]
}


Tags: 对象idtruefieldsstringrequesttypeattribute