栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用带有mysql unix时间戳的sqlalchemy定义表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用带有mysql unix时间戳的sqlalchemy定义表

我认为您显示的类型装饰器有几个问题。

  1. impl
    应该
    sqlalchemy.types.Integer
    代替
    DateTime
  2. 装饰器应允许可空列。

这就是我的想法:

import datetime, timefrom sqlalchemy.types import TypeDecorator, DateTime, Integerclass IntegerDateTime(TypeDecorator):    """a type that decorates DateTime, converts to unix time on    the way in and to datetime.datetime objects on the way out."""    impl = Integer # In schema, you want these datetimes to        # be stored as integers.    def process_bind_param(self, value, _):        """Assumes a datetime.datetime"""        if value is None: return None # support nullability        elif isinstance(value, datetime.datetime): return int(time.mktime(value.timetuple()))        raise ValueError("Can operate only on datetime values. "   "Offending value type: {0}".format(type(value).__name__))    def process_result_value(self, value, _):        if value is not None: # support nullability return datetime.datetime.fromtimestamp(float(value))


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659826.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号