Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorl-thumbnail in Django admin using AdminImageMixin

I'm trying to use sorl-thumbnail v11.12 to display thumbnails in the Django admin.

My models.py already uses sorl.thumbnail.ImageField as per the docs:

Note You do not need to use the sorl.thumbnail.ImageField to use sorl.thumbnail. The standard django.db.models.ImageField is fine except that using the sorl.thumbnail.ImageField lets you plugin the nice admin addition explained in the next section.

However, I am unsure how to configure the admin.py. I've already done a manage.py syncdb. Currently, I have the following, but it is not displaying the thumbnails in Django admin.

MyProject/admin.py

from django.contrib.admin import *
from sorl.thumbnail.admin import AdminImageMixin

class ModelAdmin(AdminImageMixin, ModelAdmin):
    pass

class TabularInline(AdminImageMixin, TabularInline):
    pass

class StackedInline(AdminImageMixin, StackedInline):
    pass

MyProject/myapp/admin.py

from MyProject import admin
from myapp.models import Tours

class ToursAdmin(admin.ModelAdmin):
    list_display = ('name', 'image', 'price')
    search_fields = ('name',)

admin.site.register(Tours, ToursAdmin)
like image 242
snakesNbronies Avatar asked Feb 19 '26 16:02

snakesNbronies


1 Answers

# models.py
class Tours(models.Model):
    image = models.ImageField(upload_to="path/")

    def thumb(self):
        return u'<img src="%s" />' % (get_thumbnail(c, "50x50", crop='center', quality=95).url)
    thumbs.short_description = 'Photos'
    thumbs.allow_tags = True

# admin.py
class ToursAdmin(admin.ModelAdmin):
    list_display = ('name', 'thumb', 'price')
like image 137
Konstantin Petrov Avatar answered Feb 27 '26 10:02

Konstantin Petrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!