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