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