我有一个要保存在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

最佳答案

你不能做这个:

newImg1  = resizeImg(self.img1, (75, 112))
self.img1_thumb = newImg1


您正在将图像分配给需要文件的字段。您需要首先从图像创建文件。无需再次全部输入,请参见this snippet,其中以正确的方式进行了详细说明。

您也可以使用任何thumbnailing applications来简化代码。 solr-thumbnail是最受欢迎的之一。

关于python - 覆盖模型save()在Django Admin中导致错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9496528/

10-12 17:43