| 575 | FileField and FieldFile |
| 576 | ~~~~~~~~~~~~~~~~~~~~~~~ |
| 577 | |
| 578 | Internally, :class:`FileField` instances use a utility class called |
| 579 | :class:`FieldFile` to handle the connection between the underlying file and |
| 580 | the field instance's attributes. In general you won't need to work with the |
| 581 | :class:`FieldFile` class directly, however several convenience methods on |
| 582 | :class:`FieldFile` are exposed for general usage when you use a |
| 583 | :class:`FileField` on your model: |
| 584 | |
| 585 | .. method:: FieldFile.open(mode='rb') |
| 586 | |
| 587 | Behaves like the standard Python ``open()`` method and opens the file |
| 588 | associated with this instance in the mode specified by ``mode``. |
| 589 | |
| 590 | .. method:: FieldFile.close() |
| 591 | |
| 592 | Behaves like the standard Python ``file.close()`` method and closes the file |
| 593 | associated with this instance. |
| 594 | |
| 595 | .. method:: FieldFile.save(name, content, save=True) |
| 596 | |
| 597 | This method takes a filename and file contents and passes them to the storage |
| 598 | class for this instance, then associates the stored file with this |
| 599 | instance. If you want to manually associate files with :class:`FileField` |
| 600 | instances on your model, this is the method to use. |
| 601 | |
| 602 | Takes two required arguments: ``name`` which is the name of the file, and |
| 603 | ``content`` which is a file-like object containing the file's contents. The |
| 604 | optional ``save`` argument controls whether or not the instance is saved after |
| 605 | the file has been altered. Defaults to ``True``. |
| 606 | |
| 607 | .. method:: FieldFile.delete(save=True) |
| 608 | |
| 609 | Deletes the file associated with this instance and clears all attributes on |
| 610 | the field. Note: This method will close the file if it happens to be open when |
| 611 | ``delete()`` is called. |
| 612 | |
| 613 | The optional ``save`` argument controls whether or not the instance is saved |
| 614 | after the file has been deleted. Defaults to ``True``. |
| 615 | |
659 | | By default, :class:`ImageField` instances are |
660 | | created as ``varchar(100)`` columns in your database. As with other fields, you |
661 | | can change the maximum length using the :attr:`~CharField.max_length` argument. |
| 703 | By default, :class:`ImageField` instances are created as ``varchar(100)`` |
| 704 | columns in your database. As with other fields, you can change the maximum |
| 705 | length using the :attr:`~CharField.max_length` argument. |