2012-01-16 3 views
1
내가 imagekit을 사용하고

: 그래서 imagekit장고 imagekit 및 제공 동적 경로

, 내가 정의한 두 개의 클래스 모델 :

class Photo(models.Model): 
    #photo_wrapper = models.ForeignKey(PhotoWrapper, blank=True, null=True) 
    original_image = models.ImageField(upload_to='static/photos') 
    thumbnail = ImageSpec([Adjust(contrast=1.2, sharpness=1.1), 
      resize.Crop(50, 50)], image_field='original_image', 
      format='JPEG', quality=90) 
    num_views = models.PositiveIntegerField(editable=False, default=0) 

    class IKOptions: 
     # This inner class is where we define the ImageKit options for the model 
     spec_module = 'myspecs.specs' 
     cache_dir = 'static/photos' 
     image_field = 'original_image' 
     save_count_as = 'num_views' 

class Country(models.Model):  
    country_name = models.CharField(max_length=250)   
    country_photo = models.ForeignKey(Photo, blank=True, null=True) 

    def __unicode__(self): 
      return '%s' % self.country_name 

문제는 각 사진은 "정적에서 생성된다는 것이다/사진 "경로. 내 의도는 "정적/사진/아르헨티나 /"국가 "아르헨티나"에 대한 동적 경로가 될 것, 예를 들어

.. 이미지와 국가 이름을 기반으로 동적 경로 썸네일을 저장하는 것입니다

어떻게하면됩니까?

답변

1

ImageKit의 두 가지 버전을 혼합하는 것처럼 보입니다. 최신 버전 (1.0 이상)에서는 더 이상 내부 클래스 IKOptions을 사용하지 않으므로이 모든 것은 무시됩니다. 합니다 (save_count_as 기능은 제거되었습니다.)

당신이 캐시 파일 이름을 제어하려는 경우는 ImageSpec 생성자는 cache_to kwarg 같은 ImageFieldupload_to가 호출 될 - 수 받아들입니다.

Specifies the filename to use when saving the image 
cache file. This is modeled after ImageField's ``upload_to`` and 
can be either a string (that specifies a directory) or a 
callable (that returns a filepath). Callable values should 
accept the following arguments: 

    - instance -- The model instance this spec belongs to 
    - path -- The path of the original image 
    - specname -- the property name that the spec is bound to on 
     the model instance 
    - extension -- A recommended extension. If the format of the 
     spec is set explicitly, this suggestion will be 
     based on that format. if not, the extension of the 
     original file will be passed. You do not have to use 
     this extension, it's only a recommendation. 

그래서 그냥 그 인수를 허용하고 원하는 경로를 반환하는 함수를 작성해야하고, 그래서 같은 모델의 것을 사용합니다 :

class Photo(models.Model): 
    thumbnail = ImageSpec(..., cache_to=my_cache_to_function) 
다음 cache_to 현재 문서입니다