I have created a model in Django in which I want to input creation time by default in local time. I have set the following in settings.py:
TIME_ZONE = 'Asia/Kolkata'
USE_TZ = True
I have tried the following:
import pytz
status_timezone = pytz.timezone('Etc/UTC')
class Status(models.Model):
status = models.IntegerField(default=0, blank=True)
active = models.IntegerField(default=1, blank=True)
creation_date = models.DateTimeField(default=status_timezone.localize(datetime.now()))
This always gives the same datetime for all entries (time of model initialization
class Status(models.Model):
status = models.IntegerField(default=0, blank=True)
active = models.IntegerField(default=1, blank=True)
creation_date = models.DateTimeField(default=datetime.now)
This gives datetime in UTC
How to input datetime in IST by default?
As explained in the Django docs the auto_now_add is populated with the default time zone. As described here this is populated according to
TIME_ZONE
settings.Hence - I think the answer to your question would be:
TIME_ZONE=Asia/Kolkota
creation_date = models.DateTimeField(auto_add_now=True)