Opened 10 years ago

Closed 10 years ago

#8586 closed (invalid)

ImageFieldFile cannot be saved with

Reported by: rokclimb15 Owned by: nobody
Component: File uploads/storage Version: master
Severity: Keywords:
Cc: martin@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Both of these forms of data are rejected by get_image_dimensions, but accepted by If you use an object with attr read, like get_image_dimensions wants, it is rejected by the default file storage since it doesn't have a chunk method. This is very useful if you want to duplicate an image behind the scenes for manipulation.

This may be related to #8208, since it is making this call before the image data has been written to the filesystem. It may need to be called after the ancestor save to get the proper filename (and fix #8208).


File "/usr/lib/python2.5/site-packages/django/core/handlers/" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/" in root
  173.                 return self.model_page(request, *url.split('/', 2))
File "/usr/lib/python2.5/site-packages/django/views/decorators/" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/" in model_page
  192.         return admin_obj(request, rest_of_url)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/" in __call__
  191.             return self.change_view(request, unquote(url))
File "/usr/lib/python2.5/site-packages/django/db/" in _commit_on_success
  238.                 res = func(*args, **kw)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/" in change_view
  570.                 self.save_model(request, new_object, form, change=True)
File "/usr/lib/python2.5/site-packages/django/contrib/admin/" in save_model
File "/media/MORTON USB/sites/btevents/../btevents/photos/" in save
  19.   ,, save=False)
File "/usr/lib/python2.5/site-packages/django/db/models/fields/" in save
  262.         self._dimensions_cache = get_image_dimensions(content)
File "/usr/lib/python2.5/site-packages/django/core/files/" 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

Here is a snippet to reproduce the problem.

class Photo(models.Model):
    image = models.ImageField(upload_to='event_photos')
    slideshow_image = models.ImageField(upload_to='slideshow', editable=False)

    def save(self):
        # save a stub image to get the filename we want from the FileStorage object,, save=False)
        i =
        image = i.crop((0, 0, 450, 300)), 'JPEG')
        super(Photo, self).save()

Change History (3)

comment:1 Changed 10 years ago by Kyle Fox

Should've you be passing the File object into, rather than the result of read()?

I haven't tested this, but my understanding is that your statement should be:, self.image, save=False)

comment:2 Changed 10 years ago by anonymous

Cc: martin@… added

comment:3 Changed 10 years ago by Chris Beaven

Resolution: invalid
Status: newclosed

kylefox is correct - you should be passing the File object in rather than the result of read() (and see #8501 for extending file storage so it handles saving basic file-like objects too)

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