2008-08-23 22:25:40 +00:00
|
|
|
==============
|
|
|
|
Managing files
|
|
|
|
==============
|
|
|
|
|
|
|
|
This document describes Django's file access APIs.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
class Car(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
price = models.DecimalField(max_digits=5, decimal_places=2)
|
|
|
|
photo = models.ImageField(upload_to='cars')
|
|
|
|
|
|
|
|
Any ``Car`` instance will have a ``photo`` attribute that you can use to get at
|
|
|
|
the details of the attached photo::
|
|
|
|
|
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
|
|
|
|
<ImageFieldFile: chevy.jpg>
|
|
|
|
>>> car.photo.name
|
2008-08-29 05:15:49 +00:00
|
|
|
u'cars/chevy.jpg'
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> car.photo.path
|
|
|
|
u'/media/cars/chevy.jpg'
|
|
|
|
>>> car.photo.url
|
|
|
|
u'http://media.example.com/cars/chevy.jpg'
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
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
|
|
|
|
needs to represent a file. This object is a thin wrapper around Python's
|
|
|
|
`built-in file object`_ with some Django-specific additions.
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2009-12-26 06:37:26 +00:00
|
|
|
.. _built-in file object: http://docs.python.org/library/stdtypes.html#bltin-file-objects
|
2008-08-23 22:25:40 +00:00
|
|
|
|
|
|
|
Most of the time you'll simply use a ``File`` that Django's given you (i.e. a
|
|
|
|
file attached to a model as above, or perhaps an uploaded file).
|
|
|
|
|
|
|
|
If you need to construct a ``File`` yourself, the easiest way is to create one
|
|
|
|
using a Python built-in ``file`` object::
|
|
|
|
|
|
|
|
>>> from django.core.files import File
|
|
|
|
|
|
|
|
# Create a Python file object using open()
|
|
|
|
>>> f = open('/tmp/hello.world', 'w')
|
|
|
|
>>> 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.
|
|
|
|
The following approach may be used to close files automatically::
|
|
|
|
|
|
|
|
>>> from django.core.files import File
|
|
|
|
|
|
|
|
# Create a Python file object using open() and the with statement
|
|
|
|
>>> with open('/tmp/hello.world', 'w') as f:
|
|
|
|
>>> myfile = File(f)
|
|
|
|
>>> for line in myfile:
|
|
|
|
>>> print line
|
|
|
|
>>> myfile.closed
|
|
|
|
True
|
|
|
|
>>> f.closed
|
|
|
|
True
|
|
|
|
|
|
|
|
Closing files is especially important when accessing file fields in a loop
|
|
|
|
over a large number of objects:: If files are not manually closed after
|
|
|
|
accessing them, the risk of running out of file descriptors may arise. This
|
|
|
|
may lead to the following error:
|
|
|
|
|
|
|
|
IOError: [Errno 24] Too many open files
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Django's default file storage is given by the :setting:`DEFAULT_FILE_STORAGE`
|
|
|
|
setting; if you don't explicitly provide a storage system, this is the one that
|
|
|
|
will be used.
|
|
|
|
|
|
|
|
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
|
|
|
|
useful -- you can use the global default storage system::
|
|
|
|
|
|
|
|
>>> from django.core.files.storage import default_storage
|
2008-08-31 10:37:44 +00:00
|
|
|
>>> from django.core.files.base import ContentFile
|
2008-08-23 22:25:40 +00:00
|
|
|
|
2008-08-31 10:37:44 +00:00
|
|
|
>>> path = default_storage.save('/path/to/file', ContentFile('new content'))
|
2008-08-23 22:25:40 +00:00
|
|
|
>>> path
|
|
|
|
u'/path/to/file'
|
|
|
|
|
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()
|
|
|
|
'new content'
|
|
|
|
|
|
|
|
>>> 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
|
|
|
|
|
|
|
The built-in filesystem storage class
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
Django ships with a built-in ``FileSystemStorage`` class (defined in
|
|
|
|
``django.core.files.storage``) which implements basic local filesystem file
|
|
|
|
storage. Its initializer takes two arguments:
|
|
|
|
|
|
|
|
====================== ===================================================
|
|
|
|
Argument Description
|
|
|
|
====================== ===================================================
|
|
|
|
``location`` Optional. Absolute path to the directory that will
|
|
|
|
hold the files. If omitted, it will be set to the
|
2010-11-27 12:27:43 +00:00
|
|
|
value of your :setting:`MEDIA_ROOT` setting.
|
2008-08-23 22:25:40 +00:00
|
|
|
``base_url`` Optional. URL that serves the files stored at this
|
|
|
|
location. If omitted, it will default to the value
|
2010-11-27 12:27:43 +00:00
|
|
|
of your :setting:`MEDIA_URL` setting.
|
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.db import models
|
|
|
|
from django.core.files.storage import FileSystemStorage
|
|
|
|
|
|
|
|
fs = FileSystemStorage(location='/media/photos')
|
|
|
|
|
|
|
|
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`.
|