{{{
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.
* 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>