files.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ==============
  2. Managing files
  3. ==============
  4. .. versionadded:: 1.0
  5. This document describes Django's file access APIs.
  6. By default, Django stores files locally, using the :setting:`MEDIA_ROOT` and
  7. :setting:`MEDIA_URL` settings. The examples below assume that you're using these
  8. defaults.
  9. However, Django provides ways to write custom `file storage systems`_ that
  10. allow you to completely customize where and how Django stores files. The
  11. second half of this document describes how these storage systems work.
  12. .. _file storage systems: `File storage`_
  13. Using files in models
  14. =====================
  15. When you use a :class:`~django.db.models.FileField` or
  16. :class:`~django.db.models.ImageField`, Django provides a set of APIs you can use
  17. to deal with that file.
  18. Consider the following model, using an :class:`~django.db.models.ImageField` to
  19. 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.objects.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 :class:`django.core.files.File` instance any time it
  40. needs to represent a file. This object is a thin wrapper around Python's
  41. `built-in file object`_ with some Django-specific additions.
  42. .. _built-in file object: http://docs.python.org/library/stdtypes.html#bltin-file-objects
  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 documented attributes and methods
  52. of the :class:`~django.core.files.File` class.
  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. :doc:`/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. >>> from django.core.files.base import ContentFile
  72. >>> path = default_storage.save('/path/to/file', ContentFile('new content'))
  73. >>> path
  74. u'/path/to/file'
  75. >>> default_storage.size(path)
  76. 11
  77. >>> default_storage.open(path).read()
  78. 'new content'
  79. >>> default_storage.delete(path)
  80. >>> default_storage.exists(path)
  81. False
  82. See :doc:`/ref/files/storage` for the file storage API.
  83. The built-in filesystem storage class
  84. -------------------------------------
  85. Django ships with a built-in ``FileSystemStorage`` class (defined in
  86. ``django.core.files.storage``) which implements basic local filesystem file
  87. storage. Its initializer takes two arguments:
  88. ====================== ===================================================
  89. Argument Description
  90. ====================== ===================================================
  91. ``location`` Optional. Absolute path to the directory that will
  92. hold the files. If omitted, it will be set to the
  93. value of your :setting:`MEDIA_ROOT` setting.
  94. ``base_url`` Optional. URL that serves the files stored at this
  95. location. If omitted, it will default to the value
  96. of your :setting:`MEDIA_URL` setting.
  97. ====================== ===================================================
  98. For example, the following code will store uploaded files under
  99. ``/media/photos`` regardless of what your :setting:`MEDIA_ROOT` setting is::
  100. from django.db import models
  101. from django.core.files.storage import FileSystemStorage
  102. fs = FileSystemStorage(location='/media/photos')
  103. class Car(models.Model):
  104. ...
  105. photo = models.ImageField(storage=fs)
  106. :doc:`Custom storage systems </howto/custom-file-storage>` work the same way:
  107. you can pass them in as the ``storage`` argument to a
  108. :class:`~django.db.models.FileField`.