override save method - 'ImageFile' object has no attribute '_committed'
Asked Answered
W

2

12

I override the save method like below, and get_thumbnails and save with sorl..

But get the error '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)

Any ideas ?

Warfold answered 15/5, 2013 at 10:51 Comment(0)
W
16
self.thumbnail = get_thumbnail(self.original,
                                   '50x50',
                                   crop='center',
                                   quality=99).url

solved my problem ..

Warfold answered 15/5, 2013 at 10:56 Comment(2)
saved or solved your problem? ;) P.S don't call super save method twice. super save will return you an instance use that instance to set additional fields: instance = super(HotelPhotos, self).save(*args, **kwargs)Roup
It's better to use .name, since .url will also contain MEDIA_URL, so in the end (if your MEDIA_URL is not empty) you'll get into /media/media/cache/...Frizzy
H
0

I found a similar error recently, which occurred when updating the contents of an ImageField via the django admin.

The error message was: 'InMemoryUploadedFile' object has no attribute '_committed'

models.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

This wasn't affecting every model, I narrowed it down to this:

admin.py:

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

The solution was to either alter the admin queryset like so:

admin.py:

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

Or just to get rid of it completely, since it wasn't really needed/relevant any more.

Helot answered 26/1, 2015 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.