0

出于某种原因,一旦我尝试将 ImageKit 应用到模型上以创建 ImageField 的缩略图,就会出现错误。我正在使用适用于 Django 的 ImageKit 库,您可以在此处找到

我的代码如下:

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
4

1 回答 1

5

此错误与 ImageKit 无关。

您正在尝试在其自己的定义中引用该类。该代码将在定义类时执行,此时 ResidentialReference 不存在。

将该代码放入一个方法中,或者将其完全从类中取出。

于 2015-04-29T11:00:37.040 回答