我有一个我想在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)
调整大小图像功能
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'
在Django Admin中保存此错误
AttributeError at /admin/myapp/product/add/
_committed
更新 - 完整追溯:
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
答案 0 :(得分:6)
你不能这样做:
newImg1 = resizeImg(self.img1, (75, 112))
self.img1_thumb = newImg1
您正在将图像分配给需要文件的字段。您需要先从图像创建一个文件。不要再次输入全部内容,请参阅this snippet,其中详细说明了正确的方法。
您还可以使用任何可用的thumbnailing applications来简化代码。 solr-thumbnail是最受欢迎的广告之一。