pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.75k stars 1.57k forks source link

mongoengine ImageField AttributeError: '_io.BytesIO' object has no attribute 'thumbnail' #263

Closed rochacbruno closed 11 years ago

rochacbruno commented 11 years ago

I installed Pillow pip install Pillow

Defined my model

main_image = db.ImageField(thumbnail_size=(100, 100, True))

The upload controls appears in admin form but when I select a file and upload it the error is:

AttributeError: '_io.BytesIO' object has no attribute 'thumbnail'

File "/home/rochacbruno/quokka-env/lib/python2.7/site-packages/flask_admin/contrib/mongoengine/helpers.py", line 14, in make_thumb_args
if value.thumbnail:

value is

<FileStorage: u'crunchbang_linux_Wallpaper_uo6i6.jpg' ('image/jpeg')>

mrjoes commented 11 years ago

What's your MongoEngine version?

I'm using 0.8.3 and it works as expected.

For me, value is <ImageGridFsProxy: 51ec3e32369e09121b94697f>

rochacbruno commented 11 years ago

mongoengine==0.8.3

mrjoes commented 11 years ago

This example https://github.com/mrjoes/flask-admin/blob/master/examples/mongoengine/simple.py works for you? (Image model)

If yes, I need to figure out what's different.

rochacbruno commented 11 years ago

Same error, maybe it is something in my env, Pillow or another lib.

attributeerror _io bytesio object has no attribute thumbnail werkzeug debugger

mrjoes commented 11 years ago

Either there's difference between your MongoEngine version or I'm completely lost. I even reinstalled quokka with all dependencies into fresh virtualenv, added image field to Post model - it works as expected.

Value of the ImageField is https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/fields.py#L1252 and not _io.BytesIO. And according to MongoEngine history, it was always like that. In your case, it is raw uploaded file data.

(quokka)serge@Cheetah ~/Work/quokka/quokka> python
Python 2.7.2 (default, Oct 11 2012, 20:14:37) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mongoengine
>>> mongoengine.get_version()
'0.8.3'
>>> import pymongo
>>> pymongo.version
'2.5.2'
mrjoes commented 11 years ago

Did you change Flask-Admin to keep existing form displayed after submission?

rochacbruno commented 11 years ago

No, I did not changed anything in FlaskAdmin. My only customization on edit template is for wysiwyg editor (javascript).

Only thing I did is install Pillow instead of PIL. I will try in another v-env.

rochacbruno commented 11 years ago

I just updated flask-admin and now it works! thanks again.

mrjoes commented 11 years ago

OK, here what was happening:

  1. ImageField uses special wtforms widget to display image thumbnail if there's uploaded image for the form
  2. If there was validation error, instead of having "real" MongoEngine grid data, field had Werkzeug uploaded file wrapper object
  3. Widget was not checking what's the value and was failing.

If that's your case (form validation), it solves the issue. If you didn't have any validation errors and you was seeing this issue - I still don't know what is causing it. Save does not display form with uploaded data, it redirects user to other page. Save and add creates new, empty form, without using request data.

I added checks to prevent this. Please update and try again. Make sure you see thumbnail when you edit model.