how to use timezones for datetime fields that are foreign keys in admin interface

22 views
Skip to first unread message

dalupus

unread,
Sep 2, 2012, 8:47:31 PM9/2/12
to Django users
So basically I have a Project object which has a bunch of Items like
so:

class Project(models.Model):
name = models.CharField(max_length=60)
datetime = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.datetime)

class Item(models.Model):
name = models.CharField(max_length=60)
created = models.ForeignKey(Project)

When I look at the Project the date appears correctly in TZ format.
On the item list screen however the datetime is not formated and seems
to be in utc format like so:
2012-09-03 00:28:21.664176+00:00
so it appears it is not adjusting to display the field in the users
timezone.

Any ideas how to get around this?

Melvyn Sopacua

unread,
Sep 8, 2012, 6:55:34 AM9/8/12
to django...@googlegroups.com
On 3-9-2012 2:47, dalupus wrote:

> datetime = models.DateTimeField(auto_now_add=True)
> def __unicode__(self):
> return unicode(self.datetime)

> On the item list screen however the datetime is not formated and seems
> to be in utc format

The list display uses the unicode representation by default. See here:
<https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display>

--
Melvyn Sopacua

Aymeric Augustin

unread,
Sep 21, 2012, 10:11:40 AM9/21/12
to django...@googlegroups.com
Le lundi 3 septembre 2012 02:48:24 UTC+2, dalupus a écrit :
On the item list screen however the datetime is not formated and seems
to be in utc format like so:
2012-09-03 00:28:21.664176+00:00
so it appears it is not adjusting to display the field in the users
timezone.

Any ideas how to get around this?

In Django 1.5, you will be able to use django.utils.timezone.localtime().

Unfortunately, there's no such built-in utility in Django 1.4.

-- 
Aymeric.
Reply all
Reply to author
Forward
0 new messages