Академический Документы
Профессиональный Документы
Культура Документы
===============================
.. currentmodule:: django.core.files.storage
If you need to provide custom file storage -- a common example is storing files
on some remote system -- you can do so by defining a custom storage class.
You'll need to follow these steps:
#. Your custom storage system must be a subclass of
``django.core.files.storage.Storage``::
from django.core.files.storage import Storage
class MyStorage(Storage):
...
#. Django must be able to instantiate your storage system without any arguments.
This means that any settings should be taken from ``django.conf.settings``::
from django.conf import settings
from django.core.files.storage import Storage
class MyStorage(Storage):
def __init__(self, option=None):
if not option:
option = settings.CUSTOM_STORAGE_OPTIONS
...
#. Your storage class must implement the :meth:`_open()` and :meth:`_save()`
methods, along with any other methods appropriate to your storage class. See
below for more on these methods.
In addition, if your class provides local file storage, it must override
the ``path()`` method.
Your custom storage system may override any of the storage methods explained in
:doc:`/ref/files/storage`, but you **must** implement the following methods:
*
*
*
*
*
:meth:`Storage.delete`
:meth:`Storage.exists`
:meth:`Storage.listdir`
:meth:`Storage.size`
:meth:`Storage.url`
You'll also usually want to use hooks specifically designed for custom storage
objects. These are:
.. method:: _open(name, mode='rb')
**Required**.
Called by ``Storage.open()``, this is the actual mechanism the storage class
uses to open the file. This must return a ``File`` object, though in most cases,
you'll want to return some subclass here that implements logic specific to the
backend storage system.
.. method:: _save(name, content)
Called by ``Storage.save()``. The ``name`` will already have gone through