Managing files

    By default, Django stores files locally, using the and settings. The examples below assume that you're using thesedefaults.

    However, Django provides ways to write custom file storage systems thatallow you to completely customize where and how Django stores files. Thesecond half of this document describes how these storage systems work.

    When you use a orImageField, Django provides a set of APIs you can useto deal with that file.

    Consider the following model, using an tostore a photo:

    Any Car instance will have a photo attribute that you can use to get atthe details of the attached photo:

    1. >>> car = Car.objects.get(name="57 Chevy")
    2. >>> car.photo
    3. <ImageFieldFile: chevy.jpg>
    4. 'cars/chevy.jpg'
    5. >>> car.photo.path
    6. '/media/cars/chevy.jpg'
    7. >>> car.photo.url
    8. 'http://media.example.com/cars/chevy.jpg'

    This object — car.photo in the example — is a object, which meansit has all the methods and attributes described below.

    注解

    For example, you can change the file name by setting the file'sname to a path relative to the file storage'slocation ( if you are using the defaultFileSystemStorage):

    Internally, Django uses a instance any time itneeds to represent a file.

    Most of the time you'll simply use a File that Django's given you (i.e. afile 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 oneusing a Python built-in file object:

    1. >>> from django.core.files import File
    2.  
    3. # Create a Python file object using open()
    4. >>> myfile = File(f)

    Now you can use any of the documented attributes and methodsof the class.

    Be aware that files created in this way are not automatically closed.The following approach may be used to close files automatically:

    Closing files is especially important when accessing file fields in a loopover a large number of objects. If files are not manually closed afteraccessing them, the risk of running out of file descriptors may arise. Thismay lead to the following error:

    1. IOError: [Errno 24] Too many open files

    Django's default file storage is given by the setting; if you don't explicitly provide a storage system, this is the one thatwill be used.

    See below for details of the built-in default file storage system, and see编写一个自定义存储系统 for information on writing your own filestorage system.

    Though most of the time you'll want to use a File object (which delegates tothe 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 moreuseful — you can use the global default storage system:

    See for the file storage API.

    The built-in filesystem storage class

    Django ships with a classwhich implements basic local filesystem file storage.

    For example, the following code will store uploaded files under/media/photos regardless of what your MEDIA_ROOT setting is:

    1. from django.core.files.storage import FileSystemStorage
    2. from django.db import models
    3.  
    4. fs = FileSystemStorage(location='/media/photos')
    5.  
    6. class Car(models.Model):
    7. photo = models.ImageField(storage=fs)

    work the same way:you can pass them in as the storage argument to aFileField.