由于某些原因,一旦我尝试将ImageKit应用到模型上以创建ImageField的缩略图,我就会收到错误。我正在使用Django的ImageKit库,你可以找到here
我的代码如下:
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
答案 0 :(得分:5)
此错误与ImageKit无关。
您尝试在自己的定义中引用该类。该类代码将在定义类时执行,此时ResidentialReference不存在。
将该代码放入方法中,或者将其完全从课程中删除。