Django’s File has the following attributes and methods:
The absolute path to the file’s location on a local filesystem.
Custom file storage systems may not store files locally; files stored on these systems will have a path of None.
The URL where the file can be retrieved. This is often useful in templates; for example, a bit of a template for displaying a Car (see above) might look like:
<img src='{{ car.photo.url }}' alt='{{ car.name }}' />
Open or reopen the file (which by definition also does File.seek(0)). The mode argument allows the same values as Python's standard open().
When reopening a file, mode will override whatever mode the file was originally opened with; None means to reopen with the original mode.
Iterate over the file yielding "chunks" of a given size. chunk_size defaults to 64 KB.
This is especially useful with very large files since it allows them to be streamed off disk and avoids storing the whole file in memory.
Any File that's associated with an object (as with Car.photo, above) will also have a couple of extra methods:
Saves a new file with the file name and contents provided. This will not replace the existing file, but will create a new file and update the object to point to it. If save is True, the model's save() method will be called once the file is saved. That is, these two lines:
>>> car.photo.save('myphoto.jpg', contents, save=False)
>>> car.save()
are the same as this one line:
>>> car.photo.save('myphoto.jpg', contents, save=True)
Note that the content argument must be an instance of File or of a subclass of File.
Jul 05, 2010