Search code examples
pythondjangodjango-imagekit

NameError: name 'ResidentialReference' is not defined [Django - ImageKit]


For some reason I get an error once I try to apply ImageKit onto a model to create a thumbnail for an ImageField. I am using the ImageKit library for Django which you can find here

My code is below :

from django.db import models
from PIL import Image
import os
from django.contrib import admin
from imagekit.models import ImageSpecField
from imagekit.processors import ResizeToFill

# Create your models here.
class ResidentialReference(models.Model):
    image = models.ImageField(upload_to='images', blank="true",null="true")
    thumbnail = ImageSpecField(source='image',
                               processors=[ResizeToFill(100,50)],
                               format='JPEG',
                               options={'quality':60})

    title = models.CharField(max_length = 1000, default = 'Title here...' )
    postcode = models.TextField(max_length = 1000, default='Postcode here...')
    description = models.TextField(max_length = 12000, default = 'Technical Description here...')
    equipment = models.TextField(max_length = 1000, default = 'Equipment here...')
    output = models.TextField(max_length = 1000, default = 'Rated Output here...')
    partnership = models.TextField(max_length = 12000, default = 'Viessmann partnership details here...')



    def __unicode__(self):
        return self.title

    def create_thumb(self):
        residentialReference = ResidentialReference.objects.all()[0]
        print residentialReference.thumbnail.url
        print residentialReference.thumbnail.width

Solution

  • This error doesn't have anything to do with ImageKit.

    You're trying to reference the class inside its own definition. That code will be executed when the class is being defined, at which point ResidentialReference doesn't exist.

    Put that code into a method, or take it out of the class altogether.