From 014cc896bc266d3bb8e62942f9666a6ae2b0b8d9 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Tue, 10 May 2011 00:26:32 +0000 Subject: [PATCH] Fixes #15963 -- Misleading FileField.save documentation. Thanks for the report and patch, ejucovy. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16207 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/ref/models/fields.txt | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index d924fe1318e..bc27a48bc16 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -612,9 +612,26 @@ instances on your model, the ``save()`` method is used to persist that file data. Takes two required arguments: ``name`` which is the name of the file, and -``content`` which is a file-like object containing the file's contents. The -optional ``save`` argument controls whether or not the instance is saved after -the file has been altered. Defaults to ``True``. +``content`` which is an object containing the file's contents. The +optional ``save`` argument controls whether or not the instance is +saved after the file has been altered. Defaults to ``True``. + +Note that the ``content`` argument should be an instance of +:class:`django.core.files.File`, not Python's built-in file object. +You can construct a :class:`~django.core.files.File` from an existing +Python file object like this:: + + from django.core.files import File + # Open an existing file using Python's built-in open() + f = open('/tmp/hello.world') + myfile = File(f) + +Or you can construct one from a Python string like this:: + + from django.core.files.base import ContentFile + myfile = ContentFile("hello world") + +For more information, see :doc:`/topics/files`. .. method:: FieldFile.delete(save=True)