diff --git a/tests/file_storage/models.py b/tests/file_storage/models.py index 38675a6673c..96805f6ad59 100644 --- a/tests/file_storage/models.py +++ b/tests/file_storage/models.py @@ -7,6 +7,7 @@ and where files should be stored. import random import tempfile +from pathlib import Path from django.core.files.storage import FileSystemStorage from django.db import models @@ -31,8 +32,12 @@ class Storage(models.Model): # to make sure it only gets called once. return '%s/%s' % (random.randint(100, 999), filename) + def pathlib_upload_to(self, filename): + return Path('bar') / filename + normal = models.FileField(storage=temp_storage, upload_to='tests') custom = models.FileField(storage=temp_storage, upload_to=custom_upload_to) + pathlib_callable = models.FileField(storage=temp_storage, upload_to=pathlib_upload_to) random = models.FileField(storage=temp_storage, upload_to=random_upload_to) custom_valid_name = models.FileField( storage=CustomValidNameStorage(location=temp_storage_location), diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 892d4129a51..6edfd2902c4 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -792,6 +792,12 @@ class FileFieldStorageTests(TestCase): self.assertEqual(obj.empty.read(), b"more content") obj.empty.close() + def test_pathlib_upload_to(self): + obj = Storage() + obj.pathlib_callable.save('some_file1.txt', ContentFile('some content')) + self.assertEqual(obj.pathlib_callable.name, 'bar/some_file1.txt') + obj.random.close() + def test_random_upload_to(self): # Verify the fix for #5655, making sure the directory is only # determined once.