summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJorge Barata González <jorge.barata.gonzalez@gmail.com>2015-03-28 19:14:47 +0000
committerTim Graham <timograham@gmail.com>2015-03-30 10:39:14 -0400
commit2bddc74b42dd427711385e8a3e0b72c42f2d7b36 (patch)
tree3544192dbf20f98a384a188bb759b64c3eb585c2
parentc4e8f21a9c6f3dd8400f399bbaa0dc3295a8b9e8 (diff)
[1.8.x] Fixed #15590 -- Documented how the path of a FileField can be changed.
Thanks simon29 for report, and freakboy3742, floledermann, jacob, claudep and collinanderson for discussing the task. Backport of 931a340f1feca05b7a9f95efb9a3ba62b93b37f9 from master
-rw-r--r--docs/topics/files.txt17
1 files changed, 17 insertions, 0 deletions
diff --git a/docs/topics/files.txt b/docs/topics/files.txt
index 88f2bc9049..ce8a5c75a0 100644
--- a/docs/topics/files.txt
+++ b/docs/topics/files.txt
@@ -55,6 +55,23 @@ it has all the methods and attributes described below.
file name used on disk cannot be relied on until after the model has been
saved.
+For example, you can change the file name by setting the file's
+:attr:`~django.core.files.File.name` to a path relative to the file storage's
+location (:setting:`MEDIA_ROOT` if you are using the default
+:class:`~django.core.files.storage.FileSystemStorage`)::
+
+ >>> import os
+ >>> from django.conf import settings
+ >>> initial_path = car.photo.path
+ >>> car.photo.name = 'cars/chevy_ii.jpg'
+ >>> new_path = settings.MEDIA_ROOT + car.photo.name
+ >>> # Move the file on the filesystem
+ >>> os.rename(initial_path, new_path)
+ >>> car.save()
+ >>> car.photo.path
+ '/media/cars/chevy_ii.jpg'
+ >>> car.photo.path == new_path
+ True
The ``File`` object
===================