Opened 7 years ago

Closed 3 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: master
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


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("").fp)"name.ext", f, save=False)

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

Probeably django\core\files\ 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@…> 7 years ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by daonb <bennydaon@…>

  • milestone set to post-1.0
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to anonymous
  • Patch needs improvement unset
  • Status changed from new to assigned

Changed 7 years ago by daonb <bennydaon@…>

comment:2 Changed 7 years ago by daonb <bennydaon@…>

  • Has patch set
  • Needs documentation set
  • Status changed from assigned to new

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('').read())
>>> obj5.image
<ImageFieldFile: tests/django_logo.gif>
>>> image_data_len == obj5.image.size
>>> obj5.image.width
>>> obj5.image.height

comment:3 Changed 7 years ago by SmileyChris

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 Changed 7 years ago by SmileyChris

  • Patch needs improvement set
  • Summary changed from It is not possible to save file opened by urllib2.urlopen to file storage should allow saving of basic file-like objects
  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:6 Changed 5 years ago by adamnelson

  • Resolution set to wontfix
  • Status changed from new to closed

I believe using ContentFile already handles this case:


Feel free to reopen if not.

comment:7 Changed 4 years ago by kmike

  • Cc kmike84@… added
  • Easy pickings unset
  • Resolution wontfix deleted
  • Severity set to Normal
  • Status changed from closed to reopened
  • Type set to 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 Changed 3 years ago by CollinAnderson

  • Resolution set to duplicate
  • Status changed from reopened to closed

This has been fixed via duplicate ticket #15644.

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