Opened 17 years ago
Closed 13 years ago
#8501 closed New feature (duplicate)
file storage should allow saving of basic file-like objects
| Reported by: | 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)
Change History (9)
comment:1 by , 17 years ago
| milestone: | → post-1.0 |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
by , 17 years ago
| Attachment: | 8501v1.diff added |
|---|
comment:2 by , 17 years ago
| Has patch: | set |
|---|---|
| Needs documentation: | set |
| Status: | assigned → new |
comment:3 by , 17 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 , 17 years ago
| Patch needs improvement: | set |
|---|---|
| Summary: | It is not possible to save file opened by urllib2.urlopen → file storage should allow saving of basic file-like objects |
| Triage Stage: | Unreviewed → Accepted |
comment:6 by , 15 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
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 , 14 years ago
| Cc: | added |
|---|---|
| Easy pickings: | unset |
| Resolution: | wontfix |
| Severity: | → Normal |
| Status: | closed → reopened |
| 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 , 13 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | reopened → closed |
This has been fixed via duplicate ticket #15644.
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