file.txt 3.6 KB

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