Skip to main content

Django Advance Thumbnail

A Django app that automates thumbnail creation for image fields. It generates, updates, and deletes thumbnails based on the source image with support for custom sizes and resize methods.

Features

  • Automatic thumbnail generation on model save
  • Thread-safe for multi-threaded/parallel web servers (gunicorn, uwsgi, etc.)
  • Multiple resize methods: fit (maintain aspect ratio) or fill (exact dimensions)
  • Smart regeneration - only regenerates when source image or settings change
  • Management commands for bulk operations
  • Full Django migrations support

Installation

pip install django-advance-thumbnail

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'django_advance_thumbnail',
]

Quick Start

from django.db import models
from django_advance_thumbnail import AdvanceThumbnailField

class Product(models.Model):
    image = models.ImageField(upload_to='products/')
    thumbnail = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/',
        size=(300, 300),
        null=True,
        blank=True,
    )

Parameters

Parameter Type Default Description
source_field str required Name of the source ImageField
size tuple (300, 300) Thumbnail dimensions (width, height)
resize_method str 'fit' How to resize: 'fit', 'fill', or 'cover'
force_regenerate bool False Regenerate on every save

Resize Methods

fit (default)

Maintains aspect ratio. Thumbnail fits within the specified size but may be smaller in one dimension.

# A 400x100 image with size=(150, 150) becomes 150x37
thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(150, 150),
    resize_method='fit',  # default
)

fill / cover

Guarantees exact dimensions by cropping. Use this when you need thumbnails to be exactly the specified size.

# A 400x100 image with size=(150, 150) becomes exactly 150x150
thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(150, 150),
    resize_method='fill',  # guarantees 150x150
)

Usage Examples

Basic Usage

from django.db import models
from django_advance_thumbnail import AdvanceThumbnailField

class Article(models.Model):
    image = models.ImageField(upload_to='articles/')
    thumbnail = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/',
        size=(300, 300),
        null=True,
        blank=True,
    )

Multiple Thumbnails

class Product(models.Model):
    image = models.ImageField(upload_to='products/')

    # Small thumbnail for listings (fit mode)
    thumbnail_small = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/small/',
        size=(100, 100),
        resize_method='fit',
    )

    # Large thumbnail for detail page (exact dimensions)
    thumbnail_large = AdvanceThumbnailField(
        source_field='image',
        upload_to='thumbnails/large/',
        size=(400, 400),
        resize_method='fill',
    )

Using Constants

from django_advance_thumbnail import AdvanceThumbnailField, RESIZE_FIT, RESIZE_FILL

thumbnail = AdvanceThumbnailField(
    source_field='image',
    size=(200, 200),
    resize_method=RESIZE_FILL,
)

Management Commands

Generate Thumbnails

Generate thumbnails for existing images (use when adding field to existing models):

# All models
python manage.py generate_thumbnails

# Specific model
python manage.py generate_thumbnails --model myapp.Product

# Force regenerate existing
python manage.py generate_thumbnails --force

# Preview without changes
python manage.py generate_thumbnails --dry-run

Regenerate Thumbnails

Regenerate thumbnails after changing size or resize_method:

# Detect and regenerate changed
python manage.py regenerate_thumbnails

# Force regenerate all
python manage.py regenerate_thumbnails --force

# Clear cache first
python manage.py regenerate_thumbnails --clear-cache

Smart Regeneration

Thumbnails are only regenerated when:

  • Source image changes
  • size parameter changes
  • resize_method parameter changes
  • Thumbnail doesn't exist

This is efficient for production - no unnecessary processing.

Change detection uses the cache as a fast path only. A cache miss - a restart, an eviction, or a per-process backend such as LocMemCache - falls back to comparing the stored thumbnail name against the current source, so restarts and extra workers do not produce duplicate thumbnails. Because that fallback only sees filenames, a source replaced under its existing name is not detected without a working shared cache; run regenerate_thumbnails --force in that case.

Thread Safety

Version 2.0+ is fully thread-safe for multi-threaded web servers like gunicorn and uwsgi. The previous signal disconnect/reconnect pattern has been replaced with instance-level flags.

Requirements

  • Python >= 3.6
  • Django >= 3.0
  • Pillow >= 8.0.0

Upgrading to v2.0

See MIGRATION_GUIDE.md for breaking changes and upgrade instructions.

Contact

License

MIT License

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_advance_thumbnail-2.0.1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_advance_thumbnail-2.0.1-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file django_advance_thumbnail-2.0.1.tar.gz.

File metadata

  • Download URL: django_advance_thumbnail-2.0.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for django_advance_thumbnail-2.0.1.tar.gz
Algorithm Hash digest
SHA256 504cf6982ca55756f4b214bcc65c74e9622af4e3da5a6793c88c347895d095ce
MD5 e070af335988c1f06f32d3263cfa9f86
BLAKE2b-256 dce1010cb8f6e559a8b47b4f87fcb9cbe44368bc6fdc31f0924cc7a5df9be934

See more details on using hashes here.

File details

Details for the file django_advance_thumbnail-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_advance_thumbnail-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44c8f3c3170508b75f17aeca8984221b30e8997e313fe15b8f3bcb32913968ff
MD5 fd913ba9431e440b388b873ea47349fd
BLAKE2b-256 7b56bb9160a255cae4171829b003f45dbce215e19fa43c9adad7d7450fae80ea

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.1 This release

2 files

2.0.0

2 files

1.1.2

2 files

1.1.1

1 file

1.1.0

2 files

1.0.2

1 file

1.0.1

1 file

1.0.0

1 file

0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page