In my UserProfile model, I just added django-imagekit and the thumbnail field here to create thumbnails for me, but when i run ./manage.py makemigrations, django says that there are no changes detected, and thumbnail field is not added to the database.
Here is my code. I am using Python 3.4 and Django 1.7:
from django.db import models
from django.contrib.auth.models import User
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill
from phonenumber_field.modelfields import PhoneNumberField
def get_upload_file_name(instance, filename):
return '/'.join([instance.user_auth.email, filename])
# Create your models here.
class UserProfile(models.Model):
user_auth = models.OneToOneField(User, related_name="profile")
phone = PhoneNumberField(null=True, blank=True, verbose_name="Phone number")
birth_date = models.DateField(verbose_name="Date of Birth", null=True, blank=True)
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
('N', 'Not Specified'),
)
gender = models.CharField(
max_length=1, choices=GENDER_CHOICES, blank=False, default='N', verbose_name='Gender')
pic = models.ImageField(upload_to=get_upload_file_name,
width_field="width_field",
height_field="height_field",
null=True,
blank=True,
verbose_name="Profile Picture"
)
height_field = models.PositiveIntegerField(null=True, default=0)
width_field = models.PositiveIntegerField(null=True, default=0)
thumbnail = ImageSpecField(source='pic',
processors=[ResizeToFill(120,120)],
format='JPEG',
options={'quality': 60})
Well it's technically not a field that is written to the database. So you don't have to make any migration for whatever you're using (SQLite, MySQL, PostgreSQL, etc). When you access your UserProfile.thumbnail
it will create the image for you right then and there based on the data from the pic ImageField.
From the documentation
ImageSpecFields, on the other hand, are virtual—they add no fields to your database and don’t require a database. This is handy for a lot of reasons, but it means that the path to the image file needs to be programmatically constructed based on the source image and the spec.
If you want it to generate the thumbnail and save it then you should be using ProcessedImageField