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

ContractedBlock.gif ExpandedBlockStart.gif 代码
from django.db import models
from datetime import datetime
from 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:

 

ContractedBlock.gif ExpandedBlockStart.gif
from datetime import datetime
from 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

 

posted on 2010-09-11 18:34 clowwindy 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/clowwindy/archive/2010/09/11/Django_TIMESTAMP_Field.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值