file.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. .. _ref-files-file:
  2. The ``File`` object
  3. ===================
  4. .. currentmodule:: django.core.files
  5. .. class:: File(file_object)
  6. ``File`` attributes and methods
  7. -------------------------------
  8. Django's ``File`` has the following attributes and methods:
  9. ``File.path``
  10. ~~~~~~~~~~~~~
  11. The absolute path to the file's location on a local filesystem.
  12. :ref:`Custom file storage systems <howto-custom-file-storage>` may not store
  13. files locally; files stored on these systems will have a ``path`` of ``None``.
  14. ``File.url``
  15. ~~~~~~~~~~~~
  16. The URL where the file can be retrieved. This is often useful in :ref:`templates
  17. <topics-templates>`; for example, a bit of a template for displaying a ``Car``
  18. (see above) might look like::
  19. <img src='{{ car.photo.url }}' alt='{{ car.name }}' />
  20. ``File.size``
  21. ~~~~~~~~~~~~~
  22. The size of the file in bytes.
  23. ``File.open(mode=None)``
  24. ~~~~~~~~~~~~~~~~~~~~~~~~
  25. Open or reopen the file (which by definition also does ``File.seek(0)``). The
  26. ``mode`` argument allows the same values as Python's standard ``open()``.
  27. When reopening a file, ``mode`` will override whatever mode the file was
  28. originally opened with; ``None`` means to reopen with the original mode.
  29. ``File.read(num_bytes=None)``
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. Read content from the file. The optional ``size`` is the number of bytes to
  32. read; if not specified, the file will be read to the end.
  33. ``File.__iter__()``
  34. ~~~~~~~~~~~~~~~~~~~
  35. Iterate over the file yielding one line at a time.
  36. ``File.chunks(chunk_size=None)``
  37. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. Iterate over the file yielding "chunks" of a given size. ``chunk_size`` defaults
  39. to 64 KB.
  40. This is especially useful with very large files since it allows them to be
  41. streamed off disk and avoids storing the whole file in memory.
  42. ``File.multiple_chunks(chunk_size=None)``
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. Returns ``True`` if the file is large enough to require multiple chunks to
  45. access all of its content give some ``chunk_size``.
  46. ``File.write(content)``
  47. ~~~~~~~~~~~~~~~~~~~~~~~
  48. Writes the specified content string to the file. Depending on the storage system
  49. behind the scenes, this content might not be fully committed until ``close()``
  50. is called on the file.
  51. ``File.close()``
  52. ~~~~~~~~~~~~~~~~
  53. Close the file.
  54. Additional ``ImageField`` attributes
  55. ------------------------------------
  56. ``File.width`` and ``File.height``
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. These attributes provide the dimensions of the image.
  59. Additional methods on files attached to objects
  60. -----------------------------------------------
  61. Any ``File`` that's associated with an object (as with ``Car.photo``, above)
  62. will also have a couple of extra methods:
  63. ``File.save(name, content, save=True)``
  64. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. Saves a new file with the file name and contents provided. This will not replace
  66. the existing file, but will create a new file and update the object to point to
  67. it. If ``save`` is ``True``, the model's ``save()`` method will be called once
  68. the file is saved. That is, these two lines::
  69. >>> car.photo.save('myphoto.jpg', contents, save=False)
  70. >>> car.save()
  71. are the same as this one line::
  72. >>> car.photo.save('myphoto.jpg', contents, save=True)
  73. Note that the ``content`` argument must be an instance of
  74. :class:`File` or of a subclass of :class:`File`.
  75. ``File.delete(save=True)``
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. Remove the file from the model instance and delete the underlying file. The
  78. ``save`` argument works as above.