summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2019-08-18 11:40:11 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-08-18 20:34:58 +0200
commitd1c2e6dd04d4ddc8ace17e6abfed16ac46459d1e (patch)
tree89284f40bbda4237df30210356608d2b2d2b9ab6
parentaf69842dbd98e69519f1263eca2619c3165ba13b (diff)
Refs #28428 -- Made FileField.upload_to support pathlib.Path.
-rw-r--r--django/db/models/fields/files.py2
-rw-r--r--docs/ref/models/fields.txt11
-rw-r--r--docs/releases/3.0.txt2
-rw-r--r--tests/file_storage/models.py1
-rw-r--r--tests/file_storage/tests.py2
5 files changed, 14 insertions, 4 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py
index 8f12e80725..b03d54366a 100644
--- a/django/db/models/fields/files.py
+++ b/django/db/models/fields/files.py
@@ -302,7 +302,7 @@ class FileField(Field):
if callable(self.upload_to):
filename = self.upload_to(instance, filename)
else:
- dirname = datetime.datetime.now().strftime(self.upload_to)
+ dirname = datetime.datetime.now().strftime(str(self.upload_to))
filename = posixpath.join(dirname, filename)
return self.storage.generate_filename(filename)
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
index c267501bf4..01a56c1312 100644
--- a/docs/ref/models/fields.txt
+++ b/docs/ref/models/fields.txt
@@ -629,9 +629,10 @@ Has two optional arguments:
and can be set in two ways. In both cases, the value is passed to the
:meth:`Storage.save() <django.core.files.storage.Storage.save>` method.
- If you specify a string value, it may contain :func:`~time.strftime`
- formatting, which will be replaced by the date/time of the file upload (so
- that uploaded files don't fill up the given directory). For example::
+ If you specify a string value or a :class:`~pathlib.Path`, it may contain
+ :func:`~time.strftime` formatting, which will be replaced by the date/time
+ of the file upload (so that uploaded files don't fill up the given
+ directory). For example::
class MyModel(models.Model):
# file will be uploaded to MEDIA_ROOT/uploads
@@ -679,6 +680,10 @@ Has two optional arguments:
class MyModel(models.Model):
upload = models.FileField(upload_to=user_directory_path)
+ .. versionchanged:: 3.0
+
+ Support for :class:`pathlib.Path` was added.
+
.. attribute:: FileField.storage
A storage object, which handles the storage and retrieval of your
diff --git a/docs/releases/3.0.txt b/docs/releases/3.0.txt
index b6b3368337..2e345f6fe5 100644
--- a/docs/releases/3.0.txt
+++ b/docs/releases/3.0.txt
@@ -304,6 +304,8 @@ Models
a certain (database-dependent) limit. Values from ``1`` to ``32767`` are safe
in all databases supported by Django.
+* :attr:`.FileField.upload_to` now supports :class:`pathlib.Path`.
+
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tests/file_storage/models.py b/tests/file_storage/models.py
index 96805f6ad5..8085a8bb1a 100644
--- a/tests/file_storage/models.py
+++ b/tests/file_storage/models.py
@@ -38,6 +38,7 @@ class Storage(models.Model):
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)
+ pathlib_direct = models.FileField(storage=temp_storage, upload_to=Path('bar'))
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 6edfd2902c..1c4176014c 100644
--- a/tests/file_storage/tests.py
+++ b/tests/file_storage/tests.py
@@ -796,6 +796,8 @@ class FileFieldStorageTests(TestCase):
obj = Storage()
obj.pathlib_callable.save('some_file1.txt', ContentFile('some content'))
self.assertEqual(obj.pathlib_callable.name, 'bar/some_file1.txt')
+ obj.pathlib_direct.save('some_file2.txt', ContentFile('some content'))
+ self.assertEqual(obj.pathlib_direct.name, 'bar/some_file2.txt')
obj.random.close()
def test_random_upload_to(self):