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. So even if you set a value for this field when creating the object, it will be ignored. If you want to be able to modify this field, set the following instead of auto_now_add=True
:
- For
DateField
:default=date.today
- fromdatetime.date.today()
- For
DateTimeField
:default=timezone.now
- fromdjango.utils.timezone.now()
The default form widget for this field is a TextInput
. The admin adds a JavaScript calendar, and a shortcut for “Today”. Includes an additional invalid_date
error message key.
The options auto_now_add
, auto_now
, and default
are mutually exclusive. Any combination of these options will result in an error.
Note
As currently implemented, setting auto_now
or auto_now_add
to True
will cause the field to have editable=False
and blank=True
set.
Note
The auto_now
and auto_now_add
options will always use the date in the default timezone at the moment of creation or update. If you need something different, you may want to consider simply using your own callable default or overriding save()
instead of using auto_now
or auto_now_add
; or using a DateTimeField
instead of a DateField
and deciding how to handle the conversion from datetime to date at display time.
Please login to continue.