1

次の場所で提供されるイメージキットを使用しています: imagekit

そこで、2 つのクラス モデルを定義しました。

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 

問題は、各写真が「static/photos」パスに作成されることです。私の意図は、国名に基づいて、画像とサムネイルを動的パスで保存することです..

たとえば、国が「アルゼンチン」の場合、動的パスは「static/photos/Argentina/」になります。

どうすればそれを達成できますか?

4

1 に答える 1

1

2 つの異なるバージョンの ImageKit を混在させているようです。新しいバージョン (1.0+) では内部クラスが使用されなくなったためIKOptions、そのすべてが無視されています。(save_count_as 機能も削除されました。)

キャッシュ ファイル名を制御したい場合、コンストラクターはkwarg をImageSpec受け入れます。これはのように、呼び出し可能にすることができます。の現在のドキュメントは次のとおりです。cache_toImageFieldupload_tocache_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)
于 2012-01-30T23:39:21.723 に答える