Re: How do I create timestamp type columns in (mySQL) database?
On 27/12/2012 2:40am, Subodh Nijsure wrote:
> I am working with situation where I would like to maintain timestamp (in
> UTC) of when a record is created or updated.
I can't help with MySQL but Postgres makes it easy. If you syncdb a
models.DateTimeField, Postgres (or more correctly, the Postgres backend
in Django) makes it "timestamp with time zone"
Thereafter, to put the right data into such a column you need to make
sure you are giving it a UTC date/time.
This is how I return a UTC timestamp wherever I want one ...
import pytz
from datetime import datetime
def when():
return datetime.now(tz=pytz.utc)
For datetime arithmetic I have a little utility like this which uses
Django's timezone.make_aware() ...
def addyears(dday=None, yrs=5):
from django.utils import timezone
from calendar import leapdays
if not dday:
dday = when()
ldays = leapdays(dday.year, dday.year + yrs)
future = datetime.fromordinal(dday.toordinal() + (yrs * 365) + ldays)
return timezone.make_aware(future, pytz.utc)
And, as general practice I long ago decided to use datetimes exclusively
so I don't have to think too hard.
hth
Mike
>
> mySQL offers such facility if one declares column of type 'timestamp'
> with attributes - default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'
>
> However I haven't found way to declare a column time as timestamp in
> django (1.4).
>
> I have come across postings - http://ianrolfe.livejournal.com/36017.html
>
> that tries to define field as timestamp, but it doesn't seem to work.
>
> Is there way to declare column of type 'timestamp' in django and not
> datetime and assign some properties to the column.
>
> As a hack I can always execute alter table command, but I am not sure
> what effect that will have if I want to use admin interface to edit
> those tables.
>
> Would appreciate any help/pointer on how one can go about handling UTC
> timestamp columns in database.
>
> Thanks.
>
> -Subodh
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home