#19454: Wrong date in the fields DateTimeField with auto_now_add
-------------------------------------+-------------------------------------
     Reporter:  fizista              |      Owner:  nobody
         Type:                       |     Status:  new
  Cleanup/optimization               |    Version:  1.4
    Component:  Database layer       |   Keywords:  auto_now_add auto_now
  (models, ORM)                      |  DateTimeField
     Severity:  Normal               |  Has patch:  0
 Triage Stage:  Unreviewed           |      UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
 Example:

 {{{
 class ExampleModel(models.Model):
     date_update = models.DateTimeField(auto_now=True)
     date_create = models.DateTimeField(auto_now_add=True)
 }}}

 Let's create an object:

 {{{
 > obj = ExampleModel()
 > print obj.date_create # Should not be here already set the date of
 creation of the object?
 None
 > obj.save()
 > assert obj.date_update == obj.date_create
 *** AssertionError:
 }}}

 Conclusion:
 auto_now_add - Creates a field with unspecified date

 The documentation tells us:

 ''DateField.auto_now_add
 Automatically set the field to now when the object is first created.
 Useful for creation of timestamps. Note that the current date is always
 used; it's not just a default value that you can override.''

 If we mean by creating an object:
 {{{
 > new_object = ExampleModel() # then we should get
 > print new_object.date_create # datetime.datetime object with creation
 date
 }}}

 If, however, the creation of an object is the time to save it to the
 database, then you should get something like this:
 {{{
 > obj = ExampleModel()
 > print obj.date_create # in this case it is correct
 None
 > obj.save()
 > assert obj.date_update == obj.date_create # == True
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19454>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to