diff options
Diffstat (limited to 'tests/model_fields')
| -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 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 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) |
