1👍
✅
If you really want to intergrate thumbnail generation to the model level, use the django-imagekit
https://pypi.python.org/pypi/django-imagekit
Example:
from django.db import models
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
class Profile(models.Model):
avatar = models.ImageField(upload_to='avatars')
avatar_thumbnail = ImageSpecField(source='avatar',
processors=[ResizeToFill(100, 50)],
format='JPEG',
options={'quality': 60})
profile = Profile.objects.all()[0]
print profile.avatar_thumbnail.url # > /media/CACHE/images/982d5af84cddddfd0fbf70892b4431e4.jpg
print profile.avatar_thumbnail.width # > 100
Although, in most of the cases you shouldn’t be adding the preview to the model, since there are a lot of packages for generating thumbnails on the fly (on the template level) in Django:
- [Answer]-Django Class Based Views: How to use username instead of ID to get data from extended model
Source:stackexchange.com