[Django] #19454: Wrong date in the fields DateTimeField with auto_now_add

38 views
Skip to first unread message

Django

unread,
Dec 10, 2012, 9:06:00 AM12/10/12
to django-...@googlegroups.com
#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.

Django

unread,
Dec 10, 2012, 10:16:31 AM12/10/12
to django-...@googlegroups.com
#19454: Wrong date in the fields DateTimeField with auto_now_add
-------------------------------------+-------------------------------------
Reporter: fizista | Owner: nobody
Type: | Status: closed
Cleanup/optimization | Version: 1.4
Component: Database layer | Resolution: duplicate
(models, ORM) | Triage Stage:
Severity: Normal | Unreviewed
Keywords: auto_now_add | Needs documentation: 0
auto_now DateTimeField | Patch needs improvement: 0
Has patch: 0 | UI/UX: 0
Needs tests: 0 |
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* status: new => closed
* needs_docs: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

This is already tracked by #16745.

--
Ticket URL: <https://code.djangoproject.com/ticket/19454#comment:1>

Reply all
Reply to author
Forward
0 new messages