11

我覆盖了下面的保存方法,并使用 get_thumbnails 和 sorl 保存。

但是得到错误'ImageFile' object has no attribute '_committed'

class HotelPhotos(models.Model):
    hotel = models.ForeignKey(Hotel, related_name='photos')
    code = models.CharField(max_length=255)
    original = models.ImageField(upload_to="media")
    medium = models.ImageField(upload_to="media", null=True, editable=False)
    thumbnail = models.ImageField(upload_to="media", null=True, editable=False)

    def save(self, *args, **kwargs):

        super(HotelPhotos, self).save(*args, **kwargs)
        self.medium = get_thumbnail(self.original,
                                    '100x100',
                                    crop='center',
                                    quality=99)

        self.thumbnail = get_thumbnail(self.original,
                                       '50x50',
                                       crop='center',
                                       quality=99)

        super(HotelPhotos, self).save(*args, **kwargs)

有任何想法吗 ?

4

2 回答 2

15
self.thumbnail = get_thumbnail(self.original,
                                   '50x50',
                                   crop='center',
                                   quality=99).url

解决了我的问题..

于 2013-05-15T10:56:26.650 回答
0

我最近发现了一个类似的错误,这是通过 django 管理员更新 ImageField 的内容时发生的。

错误消息是:“InMemoryUploadedFile”对象没有属性“_committed”

模型.py:

class MyObject(models.Model):
    name = models.CharField(max_length=80, unique=True, db_index=True)
    slug = models.SlugField(max_length=80, unique=True, blank=False)
    some_image = ImageField(upload_to='uploads/some/')
    # ... deleted for brevity

这并没有影响每个模型,我将其缩小为:

管理员.py:

class MyObjectAdmin(admin.ModelAdmin):
    # ... 
    def queryset(self, request):
        return super(ShipAdmin, self).queryset(request).only('name', 'slug')

解决方案是像这样更改管理查询集:

管理员.py:

class MyObjectAdmin(admin.ModelAdmin):
    # ... 
    def queryset(self, request):
        return super(MyObjectAdmin, self).queryset(request).only('name', 'slug', 'some_image')

或者只是为了完全摆脱它,因为它不再是真正需要/相关的。

于 2015-01-26T21:58:45.357 回答