mysql的timestamp类型在django中使用
来源:互联网 发布:木木聊天软件 编辑:程序博客网 时间:2024/05/22 18:57
django刚开始用,碰到不少问题,还没来得及看,先记下。原文链接: http://www.cnblogs.com/clowwindy/archive/2010/09/11/Django_TIMESTAMP_Field.html
Django TIMESTAMP Field
I'm using django with a legacy mysql db which uses TIMESTAMP columns. Django's inspectdb recognize those fields as int fields. This caused troubles if you want to render its value into a readable date. I googled and found this solution:
http://ianrolfe.livejournal.com/36017.html
代码from django.db import modelsfrom datetime import datetimefrom time import strftime## Custom field types in here.#class UnixTimestampField(models.DateTimeField): """UnixTimestampField: creates a DateTimeField that is represented on the database as a TIMESTAMP field rather than the usual DATETIME field. """ def __init__(self, null=False, blank=False, **kwargs): super(UnixTimestampField, self).__init__(**kwargs) # default for TIMESTAMP is NOT NULL unlike most fields, so we have to # cheat a little: self.blank, self.isnull = blank, null self.null = True # To prevent the framework from shoving in "not null". def db_type(self): typ=['TIMESTAMP'] # See above! if self.isnull: typ += ['NULL'] if self.auto_created: typ += ['default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'] return ' '.join(typ) def to_python(self, value): return datetime.from_timestamp(value) def get_db_prep_value(self, value): if value==None: return None return strftime('%Y%m%d%H%M%S',value.timetuple())
It worked, until I started to use lookups on these UnixTimestampField fields(such as __gte, __lte, etc). Django will raise a TypeError if I you do .filter(somefield__lte=datetime.now()).
I made some changes to the original code, and finally the lookups works as well:
from datetime import datetimefrom time import strftime,mktime## Custom field types in here.#class UnixTimestampField(models.DateTimeField): """UnixTimestampField: creates a DateTimeField that is represented on the database as a TIMESTAMP field rather than the usual DATETIME field. """ __metaclass__ = models.SubfieldBase def __init__(self, null=False, blank=False, **kwargs): super(UnixTimestampField, self).__init__(**kwargs) # default for TIMESTAMP is NOT NULL unlike most fields, so we have to # cheat a little: self.blank, self.isnull = blank, null self.null = True # To prevent the framework from shoving in "not null". def db_type(self): typ=['TIMESTAMP'] # See above! if self.isnull: typ += ['NULL'] if self.auto_created: typ += ['default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'] return ' '.join(typ) def to_python(self, value): return datetime.fromtimestamp(value) def get_prep_value(self, value): if value==None: return None return mktime(value.timetuple()) def get_db_prep_value(self, value): if value==None: return None return value
- mysql的timestamp类型在django中使用
- MYSQL中TIMESTAMP类型的使用
- MYSQL中TIMESTAMP类型
- mysql中timestamp的使用
- mysql中timestamp的使用
- mysql中timestamp类型的应用释疑
- MYSQL中TIMESTAMP类型的默认值
- [转载]MYSQL中TIMESTAMP类型的默认值
- MySQL中TIMESTAMP类型的使用说明
- MYSQL中TIMESTAMP类型的默认值
- MYSQL中TIMESTAMP类型的默认值
- MYSQL中TIMESTAMP类型的默认值
- MYSQL中TIMESTAMP类型的默认值
- MYSQL中TIMESTAMP类型的默认值
- MySQL中特殊的时间类型--TIMESTAMP
- MYSQL中TIMESTAMP类型的默认值理解
- MySql中的Timestamp类型字段的使用
- mysql的timestamp类型使用注意
- update-script脚本语法说明
- algorithm_chapter1
- 而在Jquery中则使用$.map()、$.each()来操作数组
- ZXing生成qrCode
- Java内存的理解
- mysql的timestamp类型在django中使用
- sencha touch list css(样式) 详解
- codeforces 318 A.Even Odds B.Sereja and Array
- Maven入门之依赖-第三节
- 在windows系统中如何使用内存映射来读写文件
- 恩智浦率先推出超宽带Doherty放大器
- 带无线网卡的电脑共享网络wifi的方法
- Ubuntu下QT控制台程序无法运行的解决方案以及XTerm的配置方法
- Linker and Loader 读书笔记 二