2008-08-23 22:25:40 +00:00
|
|
|
==============
|
|
|
|
Managing files
|
|
|
|
==============
|
|
|
|
|
2013-03-22 10:01:51 +00:00
|
|
|
This document describes Django's file access APIs for files such as those
|
|
|
|
uploaded by a user. The lower level APIs are general enough that you could use
|
2016-01-10 16:48:16 +00:00
|
|
|
them for other purposes. If you want to handle "static files" (JS, CSS, etc.),
|
2013-03-07 19:15:39 +00:00
|
|
|
see :doc:`/howto/static-files/index`.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
|
|
|
|
:setting:`MEDIA_URL` settings. The examples below assume that you're using these
|
|
|
|
defaults.
|
|
|
|
|
|
|
|
However, Django provides ways to write custom `file storage systems`_ that
|
|
|
|
allow you to completely customize where and how Django stores files. The
|
|
|
|
second half of this document describes how these storage systems work.
|
|
|
|
|
|
|
|
.. _file storage systems: `File storage`_
|
|
|
|
|
|
|
|
Using files in models
|
|
|
|
=====================
|
|
|
|
|
|
|
|
When you use a :class:`~django.db.models.FileField` or
|
|
|
|
:class:`~django.db.models.ImageField`, Django provides a set of APIs you can use
|
|
|
|
to deal with that file.
|
|
|
|
|
2010-11-27 12:27:43 +00:00
|
|
|
Consider the following model, using an :class:`~django.db.models.ImageField` to
|
|
|
|
store a photo::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2013-05-18 12:00:52 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2023-03-01 12:35:43 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
class Car(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
price = models.DecimalField(max_digits=5, decimal_places=2)
|
2023-03-01 12:35:43 +00:00
|
|
|
photo = models.ImageField(upload_to="cars")
|
|
|
|
specs = models.FileField(upload_to="specs")
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Any ``Car`` instance will have a ``photo`` attribute that you can use to get at
|
2023-02-09 15:48:46 +00:00
|
|
|
the details of the attached photo:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2008-09-14 06:49:28 +00:00
|
|
|
>>> car = Car.objects.get(name="57 Chevy")
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> car.photo
|
2019-10-29 13:13:47 +00:00
|
|
|
<ImageFieldFile: cars/chevy.jpg>
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> car.photo.name
|
2014-03-22 20:30:49 +00:00
|
|
|
'cars/chevy.jpg'
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> car.photo.path
|
2014-03-22 20:30:49 +00:00
|
|
|
'/media/cars/chevy.jpg'
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> car.photo.url
|
2014-03-22 20:30:49 +00:00
|
|
|
'http://media.example.com/cars/chevy.jpg'
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
This object -- ``car.photo`` in the example -- is a ``File`` object, which means
|
|
|
|
it has all the methods and attributes described below.
|
|
|
|
|
2011-11-19 15:08:07 +00:00
|
|
|
.. note::
|
|
|
|
The file is saved as part of saving the model in the database, so the actual
|
|
|
|
file name used on disk cannot be relied on until after the model has been
|
|
|
|
saved.
|
|
|
|
|
2015-03-28 19:14:47 +00:00
|
|
|
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
|
2023-02-09 15:48:46 +00:00
|
|
|
:class:`~django.core.files.storage.FileSystemStorage`):
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2015-03-28 19:14:47 +00:00
|
|
|
|
|
|
|
>>> import os
|
|
|
|
>>> from django.conf import settings
|
|
|
|
>>> initial_path = car.photo.path
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> car.photo.name = "cars/chevy_ii.jpg"
|
2015-03-28 19:14:47 +00:00
|
|
|
>>> 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
|
2011-11-19 15:08:07 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
To save an existing file on disk to a :class:`~django.db.models.FileField`:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2022-01-26 14:05:33 +00:00
|
|
|
|
|
|
|
>>> from pathlib import Path
|
|
|
|
>>> from django.core.files import File
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> path = Path("/some/external/specs.pdf")
|
|
|
|
>>> car = Car.objects.get(name="57 Chevy")
|
|
|
|
>>> with path.open(mode="rb") as f:
|
2022-01-26 14:05:33 +00:00
|
|
|
... car.specs = File(f, name=path.name)
|
|
|
|
... car.save()
|
2023-03-01 12:35:43 +00:00
|
|
|
...
|
2022-01-26 14:05:33 +00:00
|
|
|
|
2019-10-25 12:57:37 +00:00
|
|
|
.. note::
|
|
|
|
|
2020-05-25 16:05:22 +00:00
|
|
|
While :class:`~django.db.models.ImageField` non-image data attributes, such
|
|
|
|
as ``height``, ``width``, and ``size`` are available on the instance, the
|
|
|
|
underlying image data cannot be used without reopening the image. For
|
2023-02-09 15:48:46 +00:00
|
|
|
example:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2019-10-25 12:57:37 +00:00
|
|
|
|
|
|
|
>>> from PIL import Image
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> car = Car.objects.get(name="57 Chevy")
|
2019-10-25 12:57:37 +00:00
|
|
|
>>> car.photo.width
|
|
|
|
191
|
|
|
|
>>> car.photo.height
|
|
|
|
287
|
|
|
|
>>> image = Image.open(car.photo)
|
|
|
|
# Raises ValueError: seek of closed file.
|
|
|
|
>>> car.photo.open()
|
|
|
|
<ImageFieldFile: cars/chevy.jpg>
|
|
|
|
>>> image = Image.open(car.photo)
|
|
|
|
>>> image
|
2019-11-11 10:31:57 +00:00
|
|
|
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=191x287 at 0x7F99A94E9048>
|
2019-10-25 12:57:37 +00:00
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The ``File`` object
|
|
|
|
===================
|
|
|
|
|
2010-12-05 07:35:10 +00:00
|
|
|
Internally, Django uses a :class:`django.core.files.File` instance any time it
|
2016-05-08 22:07:43 +00:00
|
|
|
needs to represent a file.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
Most of the time you'll use a ``File`` that Django's given you (i.e. a file
|
|
|
|
attached to a model as above, or perhaps an uploaded file).
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
If you need to construct a ``File`` yourself, the easiest way is to create one
|
2023-02-09 15:48:46 +00:00
|
|
|
using a Python built-in ``file`` object:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
>>> from django.core.files import File
|
|
|
|
|
|
|
|
# Create a Python file object using open()
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> f = open("/path/to/hello.world", "w")
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> myfile = File(f)
|
|
|
|
|
2010-12-05 07:35:10 +00:00
|
|
|
Now you can use any of the documented attributes and methods
|
|
|
|
of the :class:`~django.core.files.File` class.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-06-07 12:08:46 +00:00
|
|
|
Be aware that files created in this way are not automatically closed.
|
2023-02-09 15:48:46 +00:00
|
|
|
The following approach may be used to close files automatically:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2012-06-07 12:08:46 +00:00
|
|
|
|
|
|
|
>>> from django.core.files import File
|
|
|
|
|
|
|
|
# Create a Python file object using open() and the with statement
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> with open("/path/to/hello.world", "w") as f:
|
2013-06-15 01:28:17 +00:00
|
|
|
... myfile = File(f)
|
2023-03-01 12:35:43 +00:00
|
|
|
... myfile.write("Hello World")
|
2013-06-15 01:28:17 +00:00
|
|
|
...
|
2012-06-07 12:08:46 +00:00
|
|
|
>>> myfile.closed
|
|
|
|
True
|
|
|
|
>>> f.closed
|
|
|
|
True
|
|
|
|
|
|
|
|
Closing files is especially important when accessing file fields in a loop
|
2013-05-10 11:23:32 +00:00
|
|
|
over a large number of objects. If files are not manually closed after
|
2012-06-07 12:08:46 +00:00
|
|
|
accessing them, the risk of running out of file descriptors may arise. This
|
2023-02-09 15:48:46 +00:00
|
|
|
may lead to the following error:
|
|
|
|
|
|
|
|
.. code-block:: pytb
|
2012-06-07 12:08:46 +00:00
|
|
|
|
2019-01-28 15:01:35 +00:00
|
|
|
OSError: [Errno 24] Too many open files
|
2012-06-07 12:08:46 +00:00
|
|
|
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
File storage
|
|
|
|
============
|
|
|
|
|
|
|
|
Behind the scenes, Django delegates decisions about how and where to store files
|
|
|
|
to a file storage system. This is the object that actually understands things
|
|
|
|
like file systems, opening and reading files, etc.
|
|
|
|
|
2022-09-11 15:33:47 +00:00
|
|
|
Django's default file storage is
|
|
|
|
``'``:class:`django.core.files.storage.FileSystemStorage`\ ``'``. If you don't
|
|
|
|
explicitly provide a storage system in the ``default`` key of the
|
|
|
|
:setting:`STORAGES` setting, this is the one that will be used.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
See below for details of the built-in default file storage system, and see
|
2010-08-19 19:27:44 +00:00
|
|
|
:doc:`/howto/custom-file-storage` for information on writing your own file
|
2008-08-23 22:25:40 +00:00
|
|
|
storage system.
|
|
|
|
|
|
|
|
Storage objects
|
|
|
|
---------------
|
|
|
|
|
|
|
|
Though most of the time you'll want to use a ``File`` object (which delegates to
|
|
|
|
the proper storage for that file), you can use file storage systems directly.
|
|
|
|
You can create an instance of some custom file storage class, or -- often more
|
2023-02-09 15:48:46 +00:00
|
|
|
useful -- you can use the global default storage system:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2008-08-31 10:37:44 +00:00
|
|
|
>>> from django.core.files.base import ContentFile
|
2018-05-12 17:37:42 +00:00
|
|
|
>>> from django.core.files.storage import default_storage
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2023-03-01 12:35:43 +00:00
|
|
|
>>> path = default_storage.save("path/to/file", ContentFile(b"new content"))
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> path
|
2019-08-20 10:01:10 +00:00
|
|
|
'path/to/file'
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2008-12-22 23:21:32 +00:00
|
|
|
>>> default_storage.size(path)
|
2008-08-23 22:25:40 +00:00
|
|
|
11
|
|
|
|
>>> default_storage.open(path).read()
|
2019-08-20 10:01:10 +00:00
|
|
|
b'new content'
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
>>> default_storage.delete(path)
|
|
|
|
>>> default_storage.exists(path)
|
|
|
|
False
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
See :doc:`/ref/files/storage` for the file storage API.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2012-11-01 20:11:05 +00:00
|
|
|
.. _builtin-fs-storage:
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
The built-in filesystem storage class
|
|
|
|
-------------------------------------
|
|
|
|
|
2014-04-02 08:08:20 +00:00
|
|
|
Django ships with a :class:`django.core.files.storage.FileSystemStorage` class
|
|
|
|
which implements basic local filesystem file storage.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
For example, the following code will store uploaded files under
|
2010-11-27 12:27:43 +00:00
|
|
|
``/media/photos`` regardless of what your :setting:`MEDIA_ROOT` setting is::
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
from django.core.files.storage import FileSystemStorage
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.db import models
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2023-03-01 12:35:43 +00:00
|
|
|
fs = FileSystemStorage(location="/media/photos")
|
|
|
|
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
class Car(models.Model):
|
|
|
|
...
|
|
|
|
photo = models.ImageField(storage=fs)
|
|
|
|
|
2010-11-27 12:27:43 +00:00
|
|
|
:doc:`Custom storage systems </howto/custom-file-storage>` work the same way:
|
|
|
|
you can pass them in as the ``storage`` argument to a
|
|
|
|
:class:`~django.db.models.FileField`.
|
2020-03-31 10:12:39 +00:00
|
|
|
|
|
|
|
Using a callable
|
|
|
|
----------------
|
|
|
|
|
|
|
|
You can use a callable as the :attr:`~django.db.models.FileField.storage`
|
|
|
|
parameter for :class:`~django.db.models.FileField` or
|
|
|
|
:class:`~django.db.models.ImageField`. This allows you to modify the used
|
|
|
|
storage at runtime, selecting different storages for different environments,
|
|
|
|
for example.
|
|
|
|
|
|
|
|
Your callable will be evaluated when your models classes are loaded, and must
|
|
|
|
return an instance of :class:`~django.core.files.storage.Storage`.
|
|
|
|
|
|
|
|
For example::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models
|
|
|
|
from .storages import MyLocalStorage, MyRemoteStorage
|
|
|
|
|
|
|
|
|
|
|
|
def select_storage():
|
|
|
|
return MyLocalStorage() if settings.DEBUG else MyRemoteStorage()
|
|
|
|
|
|
|
|
|
|
|
|
class MyModel(models.Model):
|
|
|
|
my_file = models.FileField(storage=select_storage)
|
2023-01-11 09:48:57 +00:00
|
|
|
|
|
|
|
In order to set a storage defined in the :setting:`STORAGES` setting you can
|
|
|
|
use a lambda function::
|
|
|
|
|
|
|
|
from django.core.files.storage import storages
|
|
|
|
|
2023-03-01 12:35:43 +00:00
|
|
|
|
2023-01-11 09:48:57 +00:00
|
|
|
class MyModel(models.Model):
|
|
|
|
upload = models.FileField(storage=lambda: storages["custom_storage"])
|
|
|
|
|
|
|
|
.. versionchanged:: 4.2
|
|
|
|
|
|
|
|
Support for ``storages`` was added.
|