Django unicode error when removing ImageField from Admin interface
Question: When using the Clear checkbox to remove an uploaded image and clicking Save in
the Django Admin interface, I get the following result:

> TypeError at /admin/foo/bar/1/
>
> coercing to Unicode: need string or buffer, ImageFieldFile found
>
> Exception Value: coercing to Unicode: need string or buffer, ImageFieldFile
> found Exception Location: /home/ec2-user/ve/Project/lib/python2.6/site-
> packages/django/core/files/images.py in get_image_dimensions, line 47

I am aware that this is probably to do with the unicode type of the return so
have changed from `return image` to `return image.name` in the relevant
`ImageField` code but this has not solved the problem. This is the debug log:

    
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/db/transaction.py
    in inner
                  return func(*args, **kwargs) ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/contrib/admin/options.py
    in change_view
              if form.is_valid(): ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/forms/forms.py
    in is_valid
          return self.is_bound and not bool(self.errors) ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/forms/forms.py
    in _get_errors
              self.full_clean() ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/forms/forms.py
    in full_clean
          self._clean_fields() ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/forms/forms.py
    in _clean_fields
                      value = field.clean(value, initial) ...
    
    /home/user/Devel/Project/project-cms/project/forms.py in clean
          w, h = get_image_dimensions(image) ...
    
    /home/user/ve/Project/lib/python2.6/site-packages/django/core/files/images.py
    in get_image_dimensions
          file = open(file_or_path, 'rb') ...
    

The get_image_dimensions method is imported as such: from
django.core.files.images import get_image_dimensions

This is the body of the clean function:

    
    
    def clean(self, value, initial=None):
        image = super(TheImageField, self).clean(value, initial)
        if image is None: return
        w, h = get_image_dimensions(image)
        if w != TheImageField.required_width or h != TheImageField.required_height:
            error_message = self.error_messages['incorrect_size'] % (w,h,)
            raise util.ValidationError(error_message)
        return image
    


Answer: [The signature for function
`get_image_dimensions`](https://github.com/disqus/django/blob/master/django/core/files/images.py)
is this one:

    
    
    def get_image_dimensions(file_or_path, close=False)
    

`file_or_path` should be a string to a file in the disk, not an `ImageField`,
hence the error.

You can do it like this:

    
    
    def clean(self, value, initial=None):
        image = super(TheImageField, self).clean(value, initial)
        if image is None: return
        # this is where I changed the code to add image.path
        w, h = get_image_dimensions(image.path)
        if w != TheImageField.required_width or h != TheImageField.required_height:
            error_message = self.error_messages['incorrect_size'] % (w,h,)
            raise util.ValidationError(error_message)
        return image
    

Hope this helps!

