Skip to content

Commit 6bc070b

Browse files
committed
[1.1.X] Fixed django#12229 -- Added documentation of the FieldFile methods that are exposed by FileField and ImageField. Thanks to Gabriel Hurley for the draft patch.
Backport of r13202 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@13203 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 0765d4b commit 6bc070b

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

docs/ref/models/fields.txt

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,45 @@ can change the maximum length using the :attr:`~CharField.max_length` argument.
545545

546546
.. _`strftime formatting`: http://docs.python.org/library/time.html#time.strftime
547547

548+
FileField and FieldFile
549+
~~~~~~~~~~~~~~~~~~~~~~~
550+
551+
When you access a :class:`FileField` on a model, you are given an instance
552+
of :class:`FieldFile` as a proxy for accessing the underlying file. This
553+
class has several methods that can be used to interact with file data:
554+
555+
.. method:: FieldFile.open(mode='rb')
556+
557+
Behaves like the standard Python ``open()`` method and opens the file
558+
associated with this instance in the mode specified by ``mode``.
559+
560+
.. method:: FieldFile.close()
561+
562+
Behaves like the standard Python ``file.close()`` method and closes the file
563+
associated with this instance.
564+
565+
.. method:: FieldFile.save(name, content, save=True)
566+
567+
This method takes a filename and file contents and passes them to the storage
568+
class for the field, then associates the stored file with the model field.
569+
If you want to manually associate file data with :class:`FileField`
570+
instances on your model, the ``save()`` method is used to persist that file
571+
data.
572+
573+
Takes two required arguments: ``name`` which is the name of the file, and
574+
``content`` which is a file-like object containing the file's contents. The
575+
optional ``save`` argument controls whether or not the instance is saved after
576+
the file has been altered. Defaults to ``True``.
577+
578+
.. method:: FieldFile.delete(save=True)
579+
580+
Deletes the file associated with this instance and clears all attributes on
581+
the field. Note: This method will close the file if it happens to be open when
582+
``delete()`` is called.
583+
584+
The optional ``save`` argument controls whether or not the instance is saved
585+
after the file has been deleted. Defaults to ``True``.
586+
548587
``FilePathField``
549588
-----------------
550589

@@ -605,8 +644,15 @@ The admin represents this as an ``<input type="text">`` (a single-line input).
605644

606645
.. class:: ImageField(upload_to=None, [height_field=None, width_field=None, max_length=100, **options])
607646

608-
Like :class:`FileField`, but validates that the uploaded object is a valid
609-
image. Has two extra optional arguments:
647+
Inherits all attributes and methods from :class:`FileField`, but also
648+
validates that the uploaded object is a valid image.
649+
650+
In addition to the special attributes that are available for :class:`FileField`,
651+
an :class:`ImageField` also has :attr:`~django.core.files.File.height` and
652+
:attr:`~django.core.files.File.width` attributes.
653+
654+
To facilitate querying on those attributes, :class:`ImageField` has two extra
655+
optional arguments:
610656

611657
.. attribute:: ImageField.height_field
612658

@@ -618,20 +664,16 @@ image. Has two extra optional arguments:
618664
Name of a model field which will be auto-populated with the width of the
619665
image each time the model instance is saved.
620666

621-
In addition to the special attributes that are available for :class:`FileField`,
622-
an :class:`ImageField` also has ``File.height`` and ``File.width`` attributes.
623-
See :ref:`topics-files`.
624-
625667
Requires the `Python Imaging Library`_.
626668

627669
.. _Python Imaging Library: http://www.pythonware.com/products/pil/
628670

629671
.. versionadded:: 1.0
630672
The ``max_length`` argument was added in this version.
631673

632-
By default, :class:`ImageField` instances are
633-
created as ``varchar(100)`` columns in your database. As with other fields, you
634-
can change the maximum length using the :attr:`~CharField.max_length` argument.
674+
By default, :class:`ImageField` instances are created as ``varchar(100)``
675+
columns in your database. As with other fields, you can change the maximum
676+
length using the :attr:`~CharField.max_length` argument.
635677

636678
``IntegerField``
637679
----------------
@@ -844,9 +886,9 @@ define the details of how the relation works.
844886
current date/time to be chosen.
845887

846888
Instead of a dictionary this can also be a :class:`~django.db.models.Q`
847-
object for more :ref:`complex queries <complex-lookups-with-q>`. However,
848-
if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it
849-
will only have an effect on the choices available in the admin when the
889+
object for more :ref:`complex queries <complex-lookups-with-q>`. However,
890+
if ``limit_choices_to`` is a :class:`~django.db.models.Q` object then it
891+
will only have an effect on the choices available in the admin when the
850892
field is not listed in ``raw_id_fields`` in the ``ModelAdmin`` for the model.
851893

852894
.. attribute:: ForeignKey.related_name

0 commit comments

Comments
 (0)