I have a model that I want to save in Django Admin
class Product(models.Model):
# other fields
img1 = models.ImageField(upload_to='%s/%s/1/large/' % (category, prod_no))
img1_thumb = models.ImageField(upload_to='%s/%s/1/thumbnail/' % (category, prod_no), editable=False)
def save(self, *args, **kwargs):
newImg1 = resizeImg(self.img1, (75, 112))
self.img1_thumb = newImg1
super(Product, self).save(*args, **kwargs)
The resize image function
def resizeImg(image, size):
try:
if imghdr.what(image) == 'jpeg':
img = Image.open(image)
img.thumbnail(size, Image.ANTIALIAS)
# this is how to save the img
# img.save(filename + '.jpg', 'JPEG', quality=75)
return img
else:
return 'not_jpg'
except Exception, e:
return 'exception'
Saving this in Django Admin creates this error
AttributeError at /admin/myapp/product/add/
_committed
UPDATE – full traceback:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/myapp/product/add/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'bc']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
307. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
197. return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
28. return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
93. response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
24. return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/db/transaction.py" in inner
217. res = func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in add_view
882. self.save_model(request, new_object, form, change=False)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in save_model
665. obj.save()
File "/Users/rocky/Projects/BestChoose/bc/models.py" in save
66. super(Product, self).save(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save
460. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in save_base
543. for f in meta.local_fields if not isinstance(f, AutoField)]
File "/Library/Python/2.7/site-packages/django/db/models/fields/files.py" in pre_save
253. if file and not file._committed:
File "/Library/Python/2.7/site-packages/PIL-1.1.7-py2.7-macosx-10.7-intel.egg/Image.py" in __getattr__
512. raise AttributeError(name)
Exception Type: AttributeError at /admin/myapp/product/add/
Exception Value: _committed
You cannot do this:
You are assigning an Image to a field that expects a File. You need to first create a file from the image. Instead of typing it all out again, please see this snippet that details the right way.
You can also use any of the thumbnailing applications available to simplify your code. solr-thumbnail is one of the most popular.