Save file object to FileField

3,495 views
Skip to first unread message

Brian S

unread,
Dec 14, 2009, 2:24:04 PM12/14/09
to Django users
Could someone point me in the right direction on how to save a file
object to a FileField in a model? When I try assigning the file object
directly to the FileField field, I get an atrribute error:

myfile = open('/home/sample.txt', 'wb')
mymodelinstance.resultFile=myfile
mymodelinstance.save()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call
last)

/home/bstanley/projects/buildfax/<ipython console> in <module>()

/home/bstanley/projects/buildfax/bdap/models.pyc in save(self)
799 super( Report, self ).save()
800 self.serialNumber += '-' + str( self.id )
--> 801 super( Report, self ).save()
802
803 def __unicode__( self ):

/usr/lib/python2.5/site-packages/django/db/models/base.py in save
(self, force_insert, force_update)
408 raise ValueError("Cannot force both insert and
updating in "
409 "model saving.")
--> 410 self.save_base(force_insert=force_insert,
force_update=force_update)
411
412 save.alters_data = True

/usr/lib/python2.5/site-packages/django/db/models/base.py in save_base
(self, raw, cls, origin, force_insert, force_update)
471 # It does already exist, so do an UPDATE.
472 if force_update or non_pks:
--> 473 values = [(f, None, (raw and getattr
(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
474 rows = manager.filter
(pk=pk_val)._update(values)
475 if force_update and not rows:

/usr/lib/python2.5/site-packages/django/db/models/fields/files.py in
pre_save(self, model_instance, add)
248 "Returns field's value just before saving."
249 file = super(FileField, self).pre_save(model_instance,
add)
--> 250 if file and not file._committed:
251 # Commit the file to storage prior to saving the
model
252 file.save(file.name, file, save=False)

AttributeError: 'file' object has no attribute '_committed'
---------------------------------------------------------------------------

Checking the type of this field in another instance of this model, I
see that it is a FieldFile object:

type(anotherinstance.resultFile)
>> <class 'django.db.models.fields.files.FieldFile'>

...So I'm assuming I need to construct a FieldFile object from my file
object, then assign that to mymodelinstance.resultFile and then save
(), but I'd like to make sure this is the preferred approach before
going forward.

Thanks.
Brian

Brian S

unread,
Dec 17, 2009, 4:26:48 PM12/17/09
to Django users
I found the answer in this discussion:

http://groups.google.com/group/django-users/browse_frm/thread/184e5e09db1efce4/7816cbc650d8dc77

Pass in the filename and content to the FileField's save() method
directly, rather than using the model instance's save() method.

On Dec 14, 2:24 pm, Brian S <bstan...@.com> wrote:
> Could someone point me in the right direction on how to save a file
> object to a FileField in a model? When I try assigning the file object
> directly to the FileField field, I get an atrribute error:
>
> myfile = open('/home/sample.txt', 'wb')
> mymodelinstance.resultFile=myfile
> mymodelinstance.save()
>
> ---------------------------------------------------------------------------
> AttributeError                            Traceback (most recent call
> last)
>

> /home/bstanley/projects//<ipython console> in <module>()
>
> /home/bstanley/projects//bdap/models.pyc in save(self)

Reply all
Reply to author
Forward
0 new messages