我重寫保存方法像下面,和get_thumbnails與SORL保存..覆蓋保存方法 - 「鏡像文件」對象有沒有屬性「_committed」
但得到的錯誤'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)
任何想法?
保存或解決了您的問題? ;)P.S不要調用'super'保存方法兩次。 'super'保存將返回一個實例,使用該實例來設置其他字段: 'instance = super(HotelPhotos,self).save(* args,** kwargs)' –
使用'.name'更好,因爲' .url'也會包含MEDIA_URL,所以最終(如果你的MEDIA_URL不是空的話),你將進入/ media/media/cache/... –