1

I have added a model method that successfully displays the images in admin, but only in list_display, if I add it to a fieldsets with the goal of also make it display when editing or adding. It throws an error.

Exception Value:    

Unknown field(s) (thumbnail) specified for Image. Check fields/fieldsets/exclude attributes of class ImageAdmin.

I'm sure that I am doing something "illegal". Or trying to do something that doesn't make sense. Because if I go and add an image that doesn't exist yet, it can't show the image either.

I am clearly missing how this all tie together. Can someone explain?

Thank you

In my models.py

from django.utils.html import format_html

class Image(models.Model):
    image = models.ImageField(blank=True, upload_to=_image_upload)

    def thumbnail(self):
        return format_html(
            '<img src="{}" width="auto" height="250">'.format(self.image.url)
        )

admin.py

@admin.register(Image)
class ImageAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Edit or upload image', {
            'fields': ('thumbnail','...',) #throws a FieldError
        }),
    )
    list_display = ('thumbnail', '...') #works

1 Answers1

0

Try to add readonly_fields to ImageAdmin:

@admin.register(Image)
class ImageAdmin(admin.ModelAdmin):
    readonly_fields = 'thumbnail'  # ADD THIS
    fieldsets = (
        ('Edit or upload image', {
            'fields': ('thumbnail','...',) #throws a FieldError
        }),
    )
    list_display = ('thumbnail', '...') #works

If you need the thumbnail only for the admin, see how to use AdminThumbnail from django-imagekit.

Risadinha
  • 16,058
  • 2
  • 88
  • 91
  • Thanks @Risadinha your solutions worked beautifully. And the https://github.com/matthewwithanm/django-imagekit looks interesting. Thanks for sharing. –  Aug 28 '17 at 13:47