Bug in ImageFieldFile.save() ?

76 views
Skip to first unread message

Brian Morton

unread,
Aug 27, 2008, 12:37:19 AM8/27/08
to Django users
The docs (and an analysis of the code) indicate that the second
argument to save should be the content.

http://docs.djangoproject.com/en/dev/ref/files/file/#ref-files-file

I am assuming this is binary or text content to be written to the file
in question. ImageFieldFile calls get_image_dimensions(content) in
save, and get_image_dimensions assumes that the input is a file or
path that can be open()ed by the file module.

The end result is that you cannot call self.imagefield.save() in a
model without an error. Does this seem like a bug to anyone else, or
am I completely missing something? This should probably be called on
the full filename AFTER the save to the ancestor class, so it gets
fresh cache info should manipulation occur (http://
code.djangoproject.com/ticket/8208).

Traceback:
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py"
in get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in root
173. return self.model_page(request, *url.split('/',
2))
File "/usr/lib/python2.5/site-packages/django/views/decorators/
cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py"
in model_page
192. return admin_obj(request, rest_of_url)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in __call__
191. return self.change_view(request, unquote(url))
File "/usr/lib/python2.5/site-packages/django/db/transaction.py" in
_commit_on_success
238. res = func(*args, **kw)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in change_view
570. self.save_model(request, new_object, form,
change=True)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/
options.py" in save_model
367. obj.save()
File "/media/MORTON USB/sites/btevents/../btevents/photos/models.py"
in save
20.
self.slideshow_image.save(os_path.basename(self.image.path), f.read(),
save=False)
File "/usr/lib/python2.5/site-packages/django/db/models/fields/
files.py" in save
262. self._dimensions_cache = get_image_dimensions(content)
File "/usr/lib/python2.5/site-packages/django/core/files/images.py" in
get_image_dimensions
34. file = open(file_or_path, 'rb')

Exception Type: TypeError at /admin/photos/photo/9/
Exception Value: file() argument 1 must be (encoded string without
NULL bytes), not str

Marty Alchin

unread,
Aug 27, 2008, 7:23:50 AM8/27/08
to django...@googlegroups.com
On Wed, Aug 27, 2008 at 12:37 AM, Brian Morton <rokcl...@gmail.com> wrote:
> The docs (and an analysis of the code) indicate that the second
> argument to save should be the content.
>
> http://docs.djangoproject.com/en/dev/ref/files/file/#ref-files-file
>
> I am assuming this is binary or text content to be written to the file
> in question. ImageFieldFile calls get_image_dimensions(content) in
> save, and get_image_dimensions assumes that the input is a file or
> path that can be open()ed by the file module.

Yeah, looks like the documentation needs to be clearer there, because
your assumption is incorrect. The "content" argument to
FieldFile.save() is expected to be a File object itself, since it's
usually an UploadedFile. There's been talk about updating it to accept
any file-like object, which I'm working on, but it's not yet
available.

If you need to pass in raw content like that, you can use
django.core.files.base.ContentFile to make a suitable File for use
with .save().

So, yes, there's a mismatch there, but it's the documentation that's
incomplete in this case.

-Gul

Brian Morton

unread,
Aug 27, 2008, 4:03:17 PM8/27/08
to Django users
Thanks for the clarification. I will definitely look forward to
having file-like objects work this way. I'll keep my eye out for the
commit.

On Aug 27, 7:23 am, "Marty Alchin" <gulop...@gamemusic.org> wrote:
Reply all
Reply to author
Forward
0 new messages