Lapin-Blanc
Lapin-Blanc

Reputation: 1985

django.utils.timezone.now returns UTC in default TimeField

I try to prepopulate a TimeField in django_admin with the following code :

from django.utils import timezone

time_start = models.TimeField('Heure de debut',max_length=20, default=timezone.now)

I've installed pytz and also correctly set

TIME_ZONE = 'Europe/Brussels'
USE_TZ = True

and the "now" button in admin correctly sets the time if I click on it. However, it initialy shows the time in UTC (two hours before the actual time in my case)

Am I missing something and is there a way to solve this ? I don't want to use auto_now_add=False because I want to be able to change this time later...

Upvotes: 9

Views: 4890

Answers (1)

IJR
IJR

Reputation: 1338

To get Time in local timezone as set in settings.py use:

from django.utils import timezone
timezone.localtime(timezone.now())

As for use in django models see this answer here https://stackoverflow.com/a/12654998/1340421

Upvotes: 13

Related Questions