Discussion:
why created_at is greater than updated_at while inserting data in table
Ankit Khandewal
2018-11-28 07:13:03 UTC
Permalink
hello, i am using this code to update created_at and updated_at in models:
created_at = models.DateTimeField(auto_now_add=True)
updated_at= models.DateTimeField(auto_now=True,null=True)

but however there is created _at is greater than updated_at when data is
inserted:

created_at -> 2018-11-26 11:11:30.020989
updated_at -> 2018-11-26 11:11:30.019762

please help, thank you.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9b41f94a-63d9-4e6b-b0e0-f4056eb73fa6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nelson Varela
2018-11-28 14:20:33 UTC
Permalink
I think it has to do with django adding timezone.now() on each field with
auto_now on it when saving to the databse.. the code for the first field
will get timezone.now() slightly earlier then the second one
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c3ab68d5-386b-46a9-a7b3-bcb033e2ad3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ankit Khandewal
2018-11-28 14:48:59 UTC
Permalink
is there any fix to this?
Post by Ankit Khandewal
created_at = models.DateTimeField(auto_now_add=True)
updated_at= models.DateTimeField(auto_now=True,null=True)
but however there is created _at is greater than updated_at when data is
created_at -> 2018-11-26 11:11:30.020989
updated_at -> 2018-11-26 11:11:30.019762
please help, thank you.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dd230d20-c51d-4497-b46d-d692086b1bdc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ankit Khandewal
2018-11-29 04:05:30 UTC
Permalink
and with this method created_at and updated_at both arguments will trigger
the field update event with timezon.now(), is there any other method to
enter only created_at at time of inserting
Post by Ankit Khandewal
created_at = models.DateTimeField(auto_now_add=True)
updated_at= models.DateTimeField(auto_now=True,null=True)
but however there is created _at is greater than updated_at when data is
created_at -> 2018-11-26 11:11:30.020989
updated_at -> 2018-11-26 11:11:30.019762
please help, thank you.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f97cc2d-45b4-488b-a709-d9c2fb313d08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Gerson David Vizquel Alemán
2018-11-29 16:18:36 UTC
Permalink
created_at = models.DateTimeField(editable=False)updated_at = models.DateTimeField()
def save(self, *args, **kwargs): ''' On save, update timestamps '''
if not self.id:
self.created_at = timezone.now()
self.updated_at = self.created_at
self.updated_at = timezone.now()
return super(User, self).save(*args, **kwargs)



El miércoles, 28 de noviembre de 2018, 3:13:03 (UTC-4), Ankit Khandewal
Post by Ankit Khandewal
created_at = models.DateTimeField(auto_now_add=True)
updated_at= models.DateTimeField(auto_now=True,null=True)
but however there is created _at is greater than updated_at when data is
created_at -> 2018-11-26 11:11:30.020989
updated_at -> 2018-11-26 11:11:30.019762
please help, thank you.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+***@googlegroups.com.
To post to this group, send email to django-***@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0057a8c6-731d-4fd9-8fa3-48f764485ef9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...