Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - AttributeError _committed

I have a form that saves an image, and everything is working fine, but I want to be able to crop the image. However, when I used Pillow to do this, I get a strange error that doesn't really give me much to go on.

Attribute error at /userprofile/addGame/

_committed

Looking further into the traceback, the following sections were highlighted:

I'm not really sure what this error means. I think it has something to do with the form.save(committed=False), and not being able to edit files after that point, but I'm not positive. I can edit the user after that line using form.user = request.user, so I think it's the fact that I'm trying to change the image getting uploaded that is part of the issue.

like image 574
123 Avatar asked Sep 28 '26 20:09

123


1 Answers

This is an old post but I recently experienced a similar problem. The information in the question is somewhat incomplete but I had the same error.

Assuming the Error being

File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/PIL/Image.py", line 627, in __getattr__ raise AttributeError(name)

AttributeError: _committed

So the root problem lies with using PIL (Pillow). The function :

image = Image.open(self.cleaned_data['image'])

We wrongly assume that the object image would continue to have the instance of image throughout the function(clean_image in my case).

So in my code structure,

def clean_image(self):
    if self.cleaned_data['image']:
        try:
            image = Image.open(self.cleaned_data['image'])
            image.verify()
        except IOError:
            raise forms.ValidationError(_('The file is not an image'))
    return image

The error mentioned above is thrown after we execute this code. It is so because

After several of the other PIL(pillow) functions we use, we need to open the file again. Check this out in pillow documentation

As in the code above(last line),

return image

doesn't really return anything.

To fix it

Simply add ,

image = self.cleaned_data['image'] 

before the return image line. Code looks like,

    except IOError:
        raise forms.ValidationError(_('The file is not an image'))
    image = self.cleaned_data['image']    
return image
like image 190
Shivam Sharma Avatar answered Oct 01 '26 17:10

Shivam Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!