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__'
1
Herobs 2017-05-08 18:07:38 +08:00 via Android
错误信息已经说的很详细了
|
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) ``` |
3
wentian 2017-05-08 18:12:10 +08:00
V2EX 坑爹的 markdown 支持..., 看看大意就好, 不要在意排版
|
4
billlee 2017-05-08 20:47:14 +08:00
只要你的列类型是 Date 的,用 datetime.datetime.now 是没问题的啊
|