V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
char1998
V2EX  ›  问与答

一个关于 flask_sqlalchemy 的问题(求各位大佬提供意见)

  •  
  •   char1998 · 2017-05-08 17:39:45 +08:00 · 3264 次点击
    这是一个创建于 2560 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在使用 flask_sqlalchemy 中向数据库添加日期

    default=datetime.datetime.now这样是能连时间一起获取的,但是我想要的效果的单单获取日期,不带时间,我试过

    • datetime.date.day结果报
      • sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1292, "Incorrect datetime value: '<attribute 'day' of 'datetime.date' objects>' for column 'join_time' at row 1") [SQL: u'INSERT INTO music_user (email, username, _password, avata r, join_time, gender) VALUES (%s, %s, %s, %s, %s, %s)'] [parameters: ('[email protected]', 'char1998', 'pbkdf2:sha1:1000$5CkzkpB1$38c28dfaa9556649857ae6442b8cbe2e29e8890d', None, <attribute 'day' of 'datetime.date' objects>, 3)]
    • datetime.datetime.day报了
      • sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1292, "Incorrect datetime value: '<attribute 'day' of 'datetime.date' objects>' for column 'join_time' at row 1") [SQL: u'INSERT INTO music_user (email, username, _password, avata r, join_time, gender) VALUES (%s, %s, %s, %s, %s, %s)'] [parameters: ('[email protected]', 'char1998', 'pbkdf2:sha1:1000$SNKS5Gsm$ba00dd797aa0ac25049bef2dc2042790915cb8be', None, <attribute 'day' of 'datetime.date' objects>, 3)]
    • datetime.date
      • sqlalchemy.exc.StatementError: (exceptions.TypeError) Required argument 'year' (pos 1) not found [SQL: u'INSERT INTO music_user (email, username, _password, avatar, join_time, gender) VALUES (%s, %s, %s, %s, %s, %s)'] [parameters: [{'username': 'char1998' , '_password': 'pbkdf2:sha1:1000$kCRVx1le$1a8afcf0e9640b0fe3181929cc02a5e0d07e0c0d', 'email': '[email protected]', 'avatar': None}]]
    • datetime.datetime.date()
      • TypeError: descriptor 'date' of 'datetime.datetime' object needs an argument
    • datetime.datetime.date
      • AttributeError: 'method_descriptor' object has no attribute '__module__'
    4 条回复    2017-05-08 20:47:14 +08:00
    Herobs
        1
    Herobs  
       2017-05-08 18:07:38 +08:00 via Android
    错误信息已经说的很详细了
    wentian
        2
    wentian  
       2017-05-08 18:11:22 +08:00
    ```
    from datetime import datetime

    请存 datetime.now
    ```

    代码只是大意, 没有测试( 不过据我的经验, 应该没问题 )


    看你挺执着, 不妨教一招:
    凡是涉及到时间 /日期的地方(数据库), 全部存储为 INT (即 Unix 时间戳), 正常人类都是这样做的( 事实上, 我连 IP 也是存成 INT 的 )
    然后在程序层进行各种转换

    至于 why, 莫问, 你会慢慢体会到的

    对了, 可能还需要写一个工具类

    ```
    import time

    class TimeUtils(object):
    @classmethod
    def get_current_timestamp(cls):
    return int(time.time())

    @classmethod
    def get_current_timestamp_ms(cls):
    return int(time.time() * 1000)
    ```
    wentian
        3
    wentian  
       2017-05-08 18:12:10 +08:00
    V2EX 坑爹的 markdown 支持..., 看看大意就好, 不要在意排版
    billlee
        4
    billlee  
       2017-05-08 20:47:14 +08:00
    只要你的列类型是 Date 的,用 datetime.datetime.now 是没问题的啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2187 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:50 · PVG 08:50 · LAX 17:50 · JFK 20:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.