diff options
| author | Nilesh Kumar Pahari <nileshpahari@protonmail.com> | 2026-01-26 00:02:29 +0530 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2026-01-29 10:38:53 -0300 |
| commit | 77b306ea0fea54ed440a369dc38161331602386c (patch) | |
| tree | 0840154f2cb43a1f4acf036fda020b12c76d072d /tests | |
| parent | b8fda9f58019cfe10c88aae36614da952f41ac31 (diff) | |
[6.0.x] 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.
Backport of fe189dc43ab3eddbbceefb6834893b73ca60d5ed from main.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/model_fields/models.py | 10 | ||||
| -rw-r--r-- | tests/model_fields/test_filefield.py | 8 |
2 files changed, 17 insertions, 1 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py index ba8d4fa6b0..bedca1edd7 100644 --- a/tests/model_fields/models.py +++ b/tests/model_fields/models.py @@ -261,10 +261,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 diff --git a/tests/model_fields/test_filefield.py b/tests/model_fields/test_filefield.py index 57cc7365da..fbf5c837ac 100644 --- a/tests/model_fields/test_filefield.py +++ b/tests/model_fields/test_filefield.py @@ -13,7 +13,7 @@ from django.db import IntegrityError, models from django.test import TestCase, override_settings from django.test.utils import isolate_apps -from .models import Document +from .models import Document, DocumentWithTimestamp class FileFieldTests(TestCase): @@ -209,3 +209,9 @@ class FileFieldTests(TestCase): document = MyDocument(myfile="test_file.py") self.assertEqual(document.myfile.field.model, MyDocument) + + def test_upload_to_callable_sees_auto_now_add_field_value(self): + d = DocumentWithTimestamp(myfile=ContentFile(b"content", name="foo")) + d.save() + self.assertIsNotNone(d.created_at) + self.assertIs(d.myfile.name.startswith(f"{d.created_at.year}/foo"), True) |
