From fe189dc43ab3eddbbceefb6834893b73ca60d5ed Mon Sep 17 00:00:00 2001 From: Nilesh Kumar Pahari Date: Mon, 26 Jan 2026 00:02:29 +0530 Subject: Fixed #36847 -- Ensured auto_now_add fields are set on pre_save(). Regression in 94680437a45a71c70ca8bd2e68b72aa1e2eff337. 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. --- tests/model_fields/models.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests/model_fields/models.py') diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index 1d8a447dee..a594b89adb 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -262,10 +262,20 @@ class DataModel(models.Model): # FileField +def upload_to_with_date(instance, filename): + return f"{instance.created_at.year}/{filename}" + + class Document(models.Model): myfile = models.FileField(storage=temp_storage, upload_to="unused", unique=True) +# See ticket #36847. +class DocumentWithTimestamp(models.Model): + created_at = models.DateTimeField(auto_now_add=True) + myfile = models.FileField(storage=temp_storage, upload_to=upload_to_with_date) + + ############################################################################### # ImageField -- cgit v1.3