Package: django-admin-thumbnails
django-admin-thumbnailsA Django app to assist in adding thumbnails for your model's image fields to admin list views and forms in a reasonably DRY manner.RationaleWhen working with models that includeImageFields,FileFields or when usingThumbnailerImageFieldfromeasy_thumbnails, it can often be desirable to include a thumbnail preview of the field as part of the admin form, fieldset or in list views. Various methods to achieve this exist but all seem to involve a degree of duplication. I've made a few attempts to DRY out such code over the years and this library represents my most recent solution. So, please enjoy!CompatibilityI've not exhaustively tested all the below combinations, however I believe this table to be accurate.Django 1.101.112.02.12.23.03.13.24.04.14.2Python2.7✔✔3.6✔✔✔✔✔✔✔✔>= 3.8✔✔✔✔✔✔Installation$ pip install django-admin-thumbnailsUsageThe app adds fields to yourModelAdminor*Inlineclass; one for each thumbnail you want to display. These are appended to the class'sreadonly_fieldstuple (unless you specify otherwise) so they will be displayed at the bottom of your admin form, or you can include them by name in yourfieldsetsorlist_displaydefinitions.django-admin-thumbnailswill handleImageField,FileField(so you can use SVG, for example) and (ifeasy_thumbnailsis installed)ThumbnailerImageField. In the latter case a thumbnail alias will be used, which you can specify in settings.Basic usageTo create an admin thumbnail field, decorate yourModelAdminor*Inlineclass and optionally specify what to do with the newly created field.Assuming a model like the following:classPerson(models.Model):name=models.CharField('Name',max_length=100)image=models.ImageField('Image')# ...In the simplest use-case, to add a thumbnail field to the bottom of the admin form, simply decorate theModelAdminor*Inlineclass (the order of decorators is important), supplying the name of the field from which the thumbnail will be taken:importadmin_thumbnails@admin.register(models.Person)@admin_thumbnails.thumbnail('image')classPersonAdmin(admin.ModelAdmin):passThis will add a field calledimage_thumbnail(FOO_thumbnailwhereFOOis the origin field's name) to yourModelAdminor*Inlinedefinition. To override the title given to the new field, pass a second string argument to the decorator:@admin_thumbnails.thumbnail('image','Thumbnail')To add the thumbnail to the columns shown in the model's list view, add the new field name tolist_display:@admin.register(models.Person)@admin_thumbnails.thumbnail('image')classPersonAdmin(admin.ModelAdmin):list_display=('name','image_thumbnail')Or include it in yourfieldsetssimilarly:@admin.register(models.Person)@admin_thumbnails.thumbnail('image')classPersonAdmin(admin.ModelAdmin):fieldsets=((None,{'fields':('name','image_thumbnail'),}),)Using thumbnails in the list view onlyBy default the new field will be appended to thereadonly_fieldstuple – if this is undesirable (e.g. if you want to include the thumbnail in the list view butnotin the default form fields), passappend=Falseto the decorator:@admin.register(models.Person)@admin_thumbnails.thumbnail('image',append=False)classPersonAdmin(admin.ModelAdmin):list_display=('name','image_thumbnail')This isn't necessary if you're usingfieldsets, as by doing so you will control the inclusion (or omission) and position of the thumbnail field.Using a property on the model as the thumbnail sourceAs of version 0.2.7, the name passed to thethumbnaildecorator can refer to a property on the model rather than a field. For example:classPerson(models.Model):# ...@propertydefprimary_image(self):ifself.images.count():returnself.images.first().imagereturnNoneProvided the specified property returns an instance of aFieldFile(i.e. a file stored in a field that is an instance of Django'sImageField,FileField, oreasy_thumbnails'ThumbnailerImageField), this will work as normal.This also works with Django'scached_propertydecorator.Adding a background to displayed thumbnailsIf your field contains images that are designed to be shown on a dark background, you can supplybackground=Trueto the decorator to add one to the thumbnail (via CSS) when displayed:@admin_thumbnails.thumbnail('image',background=True)If you're usingeasy_thumbnailsand want to override the alias used to generate your thumbnail on a per-field basis (as opposed to using theADMIN_THUMBNAIL_THUMBNAIL_ALIASsetting; see below), you can use thealiasargument to the decorator:@admin_thumbnails.thumbnail('image',alias='admin_thumbnail_alternative')ConfigurationADMIN_THUMBNAIL_DEFAULT_LABELDefault:'Preview'Setting this overrides the default column name / title used by thumbnails.ADMIN_THUMBNAIL_FIELD_SUFFIXDefault:'_thumbnail'Setting this overrides the suffix given to newly created thumbnail fields. Change if you have collision issues with other field names you want to use. Don't forget to updatelist_displayand/orfieldsetsin yourModelAdminas necessary.ADMIN_THUMBNAIL_THUMBNAIL_ALIASDefault:'admin_thumbnail'Ifeasy_thumbnailsis installed and available, any model field usingThumbnailerImageFieldwill be resized using a thumbnail alias calledadmin_thumbnailif it's defined. You can either define this alias in your settings (more info from theeasy_thumbnailsdocumentationhere) or supply a different alias name as the value of this setting.ADMIN_THUMBNAIL_STYLEDefault:{'display': 'block', 'width': '100px', 'height': 'auto'}A dictionary of CSS property/value pairs that will be added to thestyleattribute of any thumbnail image when output in the admin. Override to supply your own styles.ADMIN_THUMBNAIL_BACKGROUND_STYLEDefault:{'background': '#000'}A dictionary of CSS property/value pairs added when thebackground=Trueoption is used (seeUsageabove). Override to supply your own styles. Note that these styles are usedin additionto any defined inADMIN_THUMBNAIL_STYLE.Development installationIf working locally on the package you can install the development tools viapip:$pipinstall-e.[dev]To lint withflake8:$flake8Issues, suggestions, contributions...are welcome on GitHub. Thanks for your interest indjango-admin-thumbnails!