是否可以使用Marshmallow模式序列化使用Postgres间隔数据类型



sqlalchemy支持这样的间隔数据类型:

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

是否可以使用棉花糖模式序列化间隔列类型?我希望有以下内容:

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

...但是间隔棉花糖不支持数据类型。棉花糖API文档提到了其他原始数据类型和字符串,但到目前为止,我还没有使其工作。

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

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

尽管它没有提供任何验证,但我能够使用字符串来使该模式用于工作:

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

您可以使用文档中所述的TimeDelta类棉花糖字段。

您可以与datetime.timedelta序列化,并使用Integer(默认为秒(。

最新更新