2
0

file.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ===================
  2. The ``File`` object
  3. ===================
  4. The :mod:`django.core.files` module and its submodules contain built-in classes
  5. for basic file handling in Django.
  6. .. currentmodule:: django.core.files
  7. The ``File`` class
  8. ==================
  9. .. class:: File(file_object, name=None)
  10. The :class:`File` class is a thin wrapper around a Python
  11. :py:term:`file object` with some Django-specific additions.
  12. Internally, Django uses this class when it needs to represent a file.
  13. :class:`File` objects have the following attributes and methods:
  14. .. attribute:: name
  15. The name of the file including the relative path from
  16. :setting:`MEDIA_ROOT`.
  17. .. attribute:: size
  18. The size of the file in bytes.
  19. .. attribute:: file
  20. The underlying :py:term:`file object` that this class wraps.
  21. .. admonition:: Be careful with this attribute in subclasses.
  22. Some subclasses of :class:`File`, including
  23. :class:`~django.core.files.base.ContentFile` and
  24. :class:`~django.db.models.fields.files.FieldFile`, may replace this
  25. attribute with an object other than a Python :py:term:`file object`.
  26. In these cases, this attribute may itself be a :class:`File`
  27. subclass (and not necessarily the same subclass). Whenever
  28. possible, use the attributes and methods of the subclass itself
  29. rather than the those of the subclass's ``file`` attribute.
  30. .. attribute:: mode
  31. The read/write mode for the file.
  32. .. method:: open(mode=None, *args, **kwargs)
  33. Open or reopen the file (which also does ``File.seek(0)``).
  34. The ``mode`` argument allows the same values
  35. as Python's built-in :func:`python:open()`. ``*args`` and ``**kwargs``
  36. are passed after ``mode`` to Python's built-in :func:`python:open`.
  37. When reopening a file, ``mode`` will override whatever mode the file
  38. was originally opened with; ``None`` means to reopen with the original
  39. mode.
  40. It can be used as a context manager, e.g. ``with file.open() as f:``.
  41. .. method:: __iter__()
  42. Iterate over the file yielding one line at a time.
  43. .. method:: chunks(chunk_size=None)
  44. Iterate over the file yielding "chunks" of a given size. ``chunk_size``
  45. defaults to 64 KB.
  46. This is especially useful with very large files since it allows them to
  47. be streamed off disk and avoids storing the whole file in memory.
  48. .. method:: multiple_chunks(chunk_size=None)
  49. Returns ``True`` if the file is large enough to require multiple chunks
  50. to access all of its content give some ``chunk_size``.
  51. .. method:: close()
  52. Close the file.
  53. In addition to the listed methods, :class:`~django.core.files.File` exposes
  54. the following attributes and methods of its ``file`` object:
  55. ``encoding``, ``fileno``, ``flush``, ``isatty``, ``newlines``, ``read``,
  56. ``readinto``, ``readline``, ``readlines``, ``seek``, ``tell``,
  57. ``truncate``, ``write``, ``writelines``, ``readable()``, ``writable()``,
  58. and ``seekable()``.
  59. .. currentmodule:: django.core.files.base
  60. The ``ContentFile`` class
  61. =========================
  62. .. class:: ContentFile(content, name=None)
  63. The ``ContentFile`` class inherits from :class:`~django.core.files.File`,
  64. but unlike :class:`~django.core.files.File` it operates on string content
  65. (bytes also supported), rather than an actual file. For example::
  66. from django.core.files.base import ContentFile
  67. f1 = ContentFile("esta frase está en español")
  68. f2 = ContentFile(b"these are bytes")
  69. .. currentmodule:: django.core.files.images
  70. The ``ImageFile`` class
  71. =======================
  72. .. class:: ImageFile(file_object, name=None)
  73. Django provides a built-in class specifically for images.
  74. :class:`django.core.files.images.ImageFile` inherits all the attributes
  75. and methods of :class:`~django.core.files.File`, and additionally
  76. provides the following:
  77. .. attribute:: width
  78. Width of the image in pixels.
  79. .. attribute:: height
  80. Height of the image in pixels.
  81. .. currentmodule:: django.core.files
  82. Additional methods on files attached to objects
  83. ===============================================
  84. Any :class:`File` that is associated with an object (as with ``Car.photo``,
  85. below) will also have a couple of extra methods:
  86. .. method:: File.save(name, content, save=True)
  87. Saves a new file with the file name and contents provided. This will not
  88. replace the existing file, but will create a new file and update the object
  89. to point to it. If ``save`` is ``True``, the model's ``save()`` method will
  90. be called once the file is saved. That is, these two lines:
  91. .. code-block:: pycon
  92. >>> car.photo.save("myphoto.jpg", content, save=False)
  93. >>> car.save()
  94. are equivalent to:
  95. .. code-block:: pycon
  96. >>> car.photo.save("myphoto.jpg", content, save=True)
  97. Note that the ``content`` argument must be an instance of either
  98. :class:`File` or of a subclass of :class:`File`, such as
  99. :class:`~django.core.files.base.ContentFile`.
  100. .. method:: File.delete(save=True)
  101. Removes the file from the model instance and deletes the underlying file.
  102. If ``save`` is ``True``, the model's ``save()`` method will be called once
  103. the file is deleted.