Opened 16 years ago

Closed 11 years ago

#8501 closed New feature (duplicate)

file storage should allow saving of basic file-like objects

Reported by: jaboja@… Owned by: anonymous
Component: File uploads/storage Version: dev
Severity: Normal Keywords: urllib, file
Cc: kmike84@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

It is not possible to save file opened by urllib2.urlopen into field of a model.
When trying to do that following way:

f = File(urlopen("http://something.com/somefile.ext").fp)
MODEL.FIELD.save("name.ext", f, save=False)

Django throws AttributeError: '_fileobject' object has no attribute 'seek'

Probeably django\core\files\base.py on line checks if class has attribute seek, but it is being definied at line 125 even when constructor gets an object that does not have such attribute (like urllib2.addinfourl.fp which is socket._fileobject).

Attachments (1)

8501v1.diff (4.5 KB ) - added by daonb <bennydaon@…> 16 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 by daonb <bennydaon@…>, 16 years ago

milestone: post-1.0
Owner: changed from nobody to anonymous
Status: newassigned

by daonb <bennydaon@…>, 16 years ago

Attachment: 8501v1.diff added

comment:2 by daonb <bennydaon@…>, 16 years ago

Has patch: set
Needs documentation: set
Status: assignednew

Patch 8501v1.diff supports passing the result of a urlopen as the content parameter for the save method of FileField and ImageField. Here's the included testing code:

>>> obj5 = Storage()
>>> image_data_len = len(urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif').read())
>>> obj5.image.save('django_logo.gif',urlopen('http://media.djangoproject.com/img/badges/djangosite80x15.gif'))
>>> obj5.image
<ImageFieldFile: tests/django_logo.gif>
>>> image_data_len == obj5.image.size
True
>>> obj5.image.width
80L
>>> obj5.image.height
15L

comment:3 by Chris Beaven, 15 years ago

Interesting. It seems this is more useful than just for urlopen - it allows saving file objects (and stringio streams) directly too. In fact, I'd write urlopen off as a side-effect and rejigg the patch tests to just use open()

comment:4 by Chris Beaven, 15 years ago

Patch needs improvement: set
Summary: It is not possible to save file opened by urllib2.urlopenfile storage should allow saving of basic file-like objects
Triage Stage: UnreviewedAccepted

comment:5 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

comment:6 by Adam Nelson, 14 years ago

Resolution: wontfix
Status: newclosed

I believe using ContentFile already handles this case:

source:django/trunk/django/core/files/base.py

Feel free to reopen if not.

comment:7 by Mikhail Korobov, 12 years ago

Cc: kmike84@… added
Easy pickings: unset
Resolution: wontfix
Severity: Normal
Status: closedreopened
Type: New feature
UI/UX: unset

I think ContentFile doesn't fully cover this case because it needs a whole file loaded to memory and it should be possible to avoid this.

comment:8 by Collin Anderson, 11 years ago

Resolution: duplicate
Status: reopenedclosed

This has been fixed via duplicate ticket #15644.

Note: See TracTickets for help on using tickets.
Back to Top