files.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. .. _topics-files:
  2. ==============
  3. Managing files
  4. ==============
  5. **New in Django development version**
  6. This document describes Django's file access APIs.
  7. By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
  8. :setting:`MEDIA_URL` settings. The examples below assume that you're using these
  9. defaults.
  10. However, Django provides ways to write custom `file storage systems`_ that
  11. allow you to completely customize where and how Django stores files. The
  12. second half of this document describes how these storage systems work.
  13. .. _file storage systems: `File storage`_
  14. Using files in models
  15. =====================
  16. When you use a :class:`~django.db.models.FileField` or
  17. :class:`~django.db.models.ImageField`, Django provides a set of APIs you can use
  18. to deal with that file.
  19. Consider the following model, using a ``FileField`` to store a photo::
  20. class Car(models.Model):
  21. name = models.CharField(max_length=255)
  22. price = models.DecimalField(max_digits=5, decimal_places=2)
  23. photo = models.ImageField(upload_to='cars')
  24. Any ``Car`` instance will have a ``photo`` attribute that you can use to get at
  25. the details of the attached photo::
  26. >>> car = Car.object.get(name="57 Chevy")
  27. >>> car.photo
  28. <ImageFieldFile: chevy.jpg>
  29. >>> car.photo.name
  30. u'cars/chevy.jpg'
  31. >>> car.photo.path
  32. u'/media/cars/chevy.jpg'
  33. >>> car.photo.url
  34. u'http://media.example.com/cars/chevy.jpg'
  35. This object -- ``car.photo`` in the example -- is a ``File`` object, which means
  36. it has all the methods and attributes described below.
  37. The ``File`` object
  38. ===================
  39. Internally, Django uses a ``django.core.files.File`` any time it needs to
  40. represent a file. This object is a thin wrapper around Python's `built-in file
  41. object`_ with some Django-specific additions.
  42. .. _built-in file object: http://docs.python.org/lib/bltin-file-objects.html
  43. Most of the time you'll simply use a ``File`` that Django's given you (i.e. a
  44. file attached to a model as above, or perhaps an uploaded file).
  45. If you need to construct a ``File`` yourself, the easiest way is to create one
  46. using a Python built-in ``file`` object::
  47. >>> from django.core.files import File
  48. # Create a Python file object using open()
  49. >>> f = open('/tmp/hello.world', 'w')
  50. >>> myfile = File(f)
  51. Now you can use any of the ``File`` attributes and methods documented in
  52. :ref:`ref-files-file`.
  53. File storage
  54. ============
  55. Behind the scenes, Django delegates decisions about how and where to store files
  56. to a file storage system. This is the object that actually understands things
  57. like file systems, opening and reading files, etc.
  58. Django's default file storage is given by the :setting:`DEFAULT_FILE_STORAGE`
  59. setting; if you don't explicitly provide a storage system, this is the one that
  60. will be used.
  61. See below for details of the built-in default file storage system, and see
  62. :ref:`howto-custom-file-storage` for information on writing your own file
  63. storage system.
  64. Storage objects
  65. ---------------
  66. Though most of the time you'll want to use a ``File`` object (which delegates to
  67. the proper storage for that file), you can use file storage systems directly.
  68. You can create an instance of some custom file storage class, or -- often more
  69. useful -- you can use the global default storage system::
  70. >>> from django.core.files.storage import default_storage
  71. >>> path = default_storage.save('/path/to/file', 'new content')
  72. >>> path
  73. u'/path/to/file'
  74. >>> default_storage.filesize(path)
  75. 11
  76. >>> default_storage.open(path).read()
  77. 'new content'
  78. >>> default_storage.delete(path)
  79. >>> default_storage.exists(path)
  80. False
  81. See :ref:`ref-files-storage` for the file storage API.
  82. The built-in filesystem storage class
  83. -------------------------------------
  84. Django ships with a built-in ``FileSystemStorage`` class (defined in
  85. ``django.core.files.storage``) which implements basic local filesystem file
  86. storage. Its initializer takes two arguments:
  87. ====================== ===================================================
  88. Argument Description
  89. ====================== ===================================================
  90. ``location`` Optional. Absolute path to the directory that will
  91. hold the files. If omitted, it will be set to the
  92. value of your ``MEDIA_ROOT`` setting.
  93. ``base_url`` Optional. URL that serves the files stored at this
  94. location. If omitted, it will default to the value
  95. of your ``MEDIA_URL`` setting.
  96. ====================== ===================================================
  97. For example, the following code will store uploaded files under
  98. ``/media/photos`` regardless of what your ``MEDIA_ROOT`` setting is::
  99. from django.db import models
  100. from django.core.files.storage import FileSystemStorage
  101. fs = FileSystemStorage(location='/media/photos')
  102. class Car(models.Model):
  103. ...
  104. photo = models.ImageField(storage=fs)
  105. :ref:`Custom storage systems <howto-custom-file-storage>` work the same way: you
  106. can pass them in as the ``storage`` argument to a ``FileField``.