Skip to content

Commit fe189dc

Browse files
nileshpaharinessita
authored andcommitted
Fixed #36847 -- Ensured auto_now_add fields are set on pre_save().
Regression in 9468043. Refs #27222. During INSERT operations, `field.pre_save()` is called to prepare values for db insertion. The `add` param must be `True` for `auto_now_add` fields to be populated. The regression commit passed `False`, causing `auto_now_add` fields to remain `None` when used by other fields, such as `upload_to` callables. Thanks Ran Benita for the report.
1 parent 2831eae commit fe189dc

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

django/db/models/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,9 @@ def _save_table(
11751175
].features.can_return_columns_from_insert
11761176
for field in insert_fields:
11771177
value = (
1178-
getattr(self, field.attname) if raw else field.pre_save(self, False)
1178+
getattr(self, field.attname)
1179+
if raw
1180+
else field.pre_save(self, add=True)
11791181
)
11801182
if hasattr(value, "resolve_expression"):
11811183
if field not in returning_fields:

docs/releases/6.0.2.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ Bugfixes
1515
to wrap below the changelist when filter elements contained long text
1616
(:ticket:`36850`).
1717

18+
* Fixed a regression in Django 6.0 where ``auto_now_add`` field values were not
19+
populated during ``INSERT`` operations, due to incorrect parameters passed to
20+
``field.pre_save()`` (:ticket:`36847`).

tests/model_fields/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,20 @@ class DataModel(models.Model):
262262
# FileField
263263

264264

265+
def upload_to_with_date(instance, filename):
266+
return f"{instance.created_at.year}/{filename}"
267+
268+
265269
class Document(models.Model):
266270
myfile = models.FileField(storage=temp_storage, upload_to="unused", unique=True)
267271

268272

273+
# See ticket #36847.
274+
class DocumentWithTimestamp(models.Model):
275+
created_at = models.DateTimeField(auto_now_add=True)
276+
myfile = models.FileField(storage=temp_storage, upload_to=upload_to_with_date)
277+
278+
269279
###############################################################################
270280
# ImageField
271281

tests/model_fields/test_filefield.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from django.test import TestCase, override_settings
1414
from django.test.utils import isolate_apps
1515

16-
from .models import Document
16+
from .models import Document, DocumentWithTimestamp
1717

1818

1919
class FileFieldTests(TestCase):
@@ -209,3 +209,9 @@ class MyDocument(AbstractMyDocument):
209209

210210
document = MyDocument(myfile="test_file.py")
211211
self.assertEqual(document.myfile.field.model, MyDocument)
212+
213+
def test_upload_to_callable_sees_auto_now_add_field_value(self):
214+
d = DocumentWithTimestamp(myfile=ContentFile(b"content", name="foo"))
215+
d.save()
216+
self.assertIsNotNone(d.created_at)
217+
self.assertIs(d.myfile.name.startswith(f"{d.created_at.year}/foo"), True)

0 commit comments

Comments
 (0)