#28671: DateTimeField: make auto_now_add just a default value
-------------------------------------+-------------------------------------
Reporter: Дилян | Owner: nobody
Палаузов |
Type: | Status: new
Uncategorized |
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Providing there is no strong reason to threat auto_now_add as something
different than a default value, that cannot be overridden, here is a patch
permitting to set the auto_now_add:
{{{
diff --git a/django/db/models/fields/__init__.py
b/django/db/models/fields/__init__.py
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -1272,7 +1272,7 @@ class DateField(DateTimeCheckMixin, Field):
)
def pre_save(self, model_instance, add):
- if self.auto_now or (self.auto_now_add and add):
+ if self.auto_now or (self.auto_now_add and add and
getattr(model_instance, self.attname, None) is None):
value = datetime.date.today()
setattr(model_instance, self.attname, value)
return value
@@ -1424,7 +1424,7 @@ class DateTimeField(DateField):
)
def pre_save(self, model_instance, add):
- if self.auto_now or (self.auto_now_add and add):
+ if self.auto_now or (self.auto_now_add and add and
getattr(model_instance, self.attname, None) is None):
value = timezone.now()
setattr(model_instance, self.attname, value)
return value
@@ -2264,7 +2264,7 @@ class TimeField(DateTimeCheckMixin, Field):
)
def pre_save(self, model_instance, add):
- if self.auto_now or (self.auto_now_add and add):
+ if self.auto_now or (self.auto_now_add and add and
getattr(model_instance, self.attname, None) is None):
value = datetime.datetime.now().time()
setattr(model_instance, self.attname, value)
return value
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
--- a/docs/ref/models/fields.txt
+++ b/docs/ref/models/fields.txt
@@ -514,9 +514,8 @@ optional arguments:
.. attribute:: 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.
+ for creation of timestamps. If you set a value for this field when
creating
+ the object, auto_now_add will be ignored.
If you want to be able to modify this field, set the following
instead of
``auto_now_add=True``:
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28671>
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/057.4c7e64d8413964df1f4cd6831c13cce4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.