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) orfill(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
sizeparameter changesresize_methodparameter 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
- Email: mh@mahadihassan.com
- GitHub: @itsmahadi007
- LinkedIn: Mahadi Hassan
- Web: mahadihassan.com
License
MIT License
Metadata
Release files for django-advance-thumbnail 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_advance_thumbnail-2.0.1.tar.gz | 19.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_advance_thumbnail-2.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.6 kB
Release files / django_advance_thumbnail-2.0.1.tar.gz
| Download URL | django_advance_thumbnail-2.0.1.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
504cf6982ca55756f4b214bcc65c74e9622af4e3da5a6793c88c347895d095ce
|
|
BLAKE2b-256 checksum How to use checksums |
dce1010cb8f6e559a8b47b4f87fcb9cbe44368bc6fdc31f0924cc7a5df9be934
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / django_advance_thumbnail-2.0.1-py3-none-any.whl
| Download URL | django_advance_thumbnail-2.0.1-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44c8f3c3170508b75f17aeca8984221b30e8997e313fe15b8f3bcb32913968ff
|
|
BLAKE2b-256 checksum How to use checksums |
7b56bb9160a255cae4171829b003f45dbce215e19fa43c9adad7d7450fae80ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for django-advance-thumbnail 2.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.