matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

Exception Value: 'Image' object has no attribute 'closed' #480

Closed thelittlebug closed 5 years ago

thelittlebug commented 5 years ago

Hello,

I am trying to add a thumbnail to the django admin list display as described in the docs.

My installed packages:

Django                     2.1.4     
django-filer               1.4.1     
django-imagekit            4.0.2     
pilkit                     2.0       
Pillow                     5.3.0     

My model and the admin config:

from django.db import models
from filer.fields.image import FilerImageField
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFit

class Trainer(models.Model):
    name = models.CharField(max_length=100)
    picture = FilerImageField(
        null=True,
        blank=True,
        related_name="picture_trainer",
        on_delete=models.CASCADE
    )
    picture_admin_thumbnail = ImageSpecField(
        source='picture',
        processors=[ResizeToFit(120)],
        format='JPEG',
        options={'quality': 70}
    )
    quote = models.CharField(max_length=200)
    experience = models.CharField(max_length=500)
    education = models.CharField(max_length=500)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
from django.contrib import admin
from imagekit.admin import AdminThumbnail

from .models import Trainer

class TrainerAdmin(admin.ModelAdmin):
    list_display = (
        '__str__',
        'picture_thumbnail',
        'name',
        'created_at',
        'updated_at'
    )
    picture_thumbnail = AdminThumbnail(image_field='picture_admin_thumbnail')
    readonly_fields = ['picture_thumbnail']

admin.site.register(Trainer, TrainerAdmin)

on my own im not able to figure out why it throws this error. any help would be of great value.

thx :)

vstoykov commented 5 years ago

Thank you for reporting the problem.

It would be good if you provided the traceback of the error in order to see where it was raised.

Nevertheless from what I know FilerImageField is actually foreign key to a model instead of a real ImageField which means that picture attribute will be Model instance instead of something extending ImageFile or at least File. The actual file from django-filer in your case is picture.file.

In order to fix this probably you should need to make a getter property in your model. For example something like:

@property
def picture_file(self):
    return picture.file

Or something similar and then use source='picture_file'.

This is untested it just came in my mind but you can try it.

thelittlebug commented 5 years ago

thank you for your valuable input on this problem.

i was unable to debug it on my own because i had no real debugging possibility till now (it's not so easy to debug django and all possible external and internal python modules as a django newcomer without a good configured dev-environment. maybe i should write a little "howto: debug django and other little monsters, the comfortably way" ).

i'll try your idea in the next few days.

many thx

thelittlebug commented 5 years ago

thank you, works as expected :)


@property
def picture_file(self):
    return self.picture.file