file.txt 3.5 KB

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