【问题标题】:Is it possible to use a Postgres Interval data type with Marshmallow schema serialization?是否可以将 Postgres Interval 数据类型与 Marshmallow 模式序列化一起使用?
【发布时间】:2018-02-07 02:38:15
【问题描述】:

SqlAlchemy 像这样支持Interval data types

class Sample(Base):
    __tablename__ = "samples"
    id = Column(Integer(), primary_key=True)
    time_interval = Column(Interval(), nullable=True)

是否可以使用 Marshmallow 模式序列化间隔列类型?我希望得到类似以下的内容:

class SampleSchema(Schema):
    id = fields.Int()
    time_interval = fields.Interval(allow_none=True)  # not supported

...但是 Marshmallow 不支持 interval 数据类型。 Marshmallow API documentation 提到了其他原始数据类型和字符串,但到目前为止我还不能让它工作。

TypeError: Object of type 'timedelta' is not JSON serializable

感谢您的任何提示或建议。

【问题讨论】:

    标签: python postgresql sqlalchemy marshmallow


    【解决方案1】:

    虽然它不提供任何验证,但我能够使用字符串使架构工作:

    class SampleSchema(Schema):
        id = fields.Int()
        time_interval = fields.String(allow_none=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 2018-08-25
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      相关资源
      最近更新 更多