Opened 7 weeks ago

Closed 7 weeks ago

Last modified 7 weeks ago

#28671 closed Cleanup/optimization (wontfix)

DateTimeField: make auto_now_add just a default value

Reported by: Дилян Палаузов Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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``:
 

Change History (2)

comment:1 Changed 7 weeks ago by Tim Graham

Resolution: wontfix
Status: newclosed
Type: UncategorizedCleanup/optimization

Is there a problem with using default=date.today or default=timezone.now (as the documentation suggests) for your use case? There's some thought to deprecate auto_now and auto_now_add (#22995), and I don't see a compelling reason to change their behavior when an alternative exists.

comment:2 Changed 7 weeks ago by Дилян Палаузов

I am in favour of deprecating instead auto_now_add, if this happens in foreseeable future.

Note: See TracTickets for help on using tickets.
Back to Top