Search code examples
pythondjangotimezonedjango-timezone

Django DateTimeField says 'You are 5.5 hours ahead of server time.'


In one of my model I am storing time_stamp = models.DateTimeField(default=timezone.now)

But when I save the model it says You are 5.5 hours ahead of server time. for example local time in my machine is 13:02 but after saving what gets stored in db is 7:16

I got one related here but it does not an have a satisfying answer...

models.py

class Comment(models.Model):
    time_stamp = models.DateTimeField(default=timezone.now)

    def save(self, *args, **kwargs):
        ''' On save, update timestamps '''
        if not self.id:
            self.time_stamp = timezone.now()
        return super(Comment, self).save(*args, **kwargs)

Solution

  • As you are 5.5 hrs ahead of server time, I am assuming, you are in India.
    So put appriopriate timezone in settings.py

    TIME_ZONE = 'Asia/Kolkata'

    If somewhere else, set accordingly