How can I set manually DateTimeField value given date and time ?
Here is my model and I am trying to assign date & time value to "created_at" field:
class Attendance(models.Model):
employee = models.ForeignKey(Employee)
company = models.ForeignKey(Company)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.employee.username
Here is something I tried in the console:
> o = Attendance.objects.last()
> o.updated_at
datetime.datetime(2018, 6, 26, 0, 45, 31, 829827, tzinfo=<UTC>)
> x = datetime.strptime('17/07/2018 12:20', '%d/%m/%Y %H:%M')
> x
datetime.datetime(2018, 7, 17, 12, 20)
>o.updated_at = x
o.save()
>z = Attendance.objects.last()
>z.updated_at
datetime.datetime(2018, 7, 29, 12, 0, 13, 204815, tzinfo=<UTC>)
notice that I set "2018, 7, 17" but it appears "2018, 7, 29". is the assignment correct ? probably it needs the timezone as well.