Django easy-thumbnails 使用重复的扩展名获得奇怪的命名

Django easy-thumbnails getting weird naming with duplicate extensions

我正在使用 easy-thumbnails,原始文件已正确保存,但缩略图正在使用重复的文件扩展名保存:

11.jpg
11.jpg.100x100_q85.jpg

我希望缩略图可以这样命名:

11.100x100_q85.jpg

我的模型是这样的:

def image_filename(instance, filename):
    folder = 'posts/image'
    _, ext = os.path.splitext(filename)
    new_name = str(instance.id) + ext
    return os.path.join(folder, new_name)


class Post(models.Model):
    name = models.CharField(max_length=255, unique=True)
    image = ThumbnailerImageField(upload_to=image_filename, null=True, blank=True)

由于我使用的是 Django Rest Framework,因此我创建了一个序列化程序 post: Django easy-thumbnails serialize with Django Rest Framework

class ThumbnailSerializer(serializers.ImageField):
    def __init__(self, alias, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.read_only = True
        self.alias = alias

    def to_representation(self, value):
        if not value:
            return None

        url = thumbnail_url(value, self.alias)
        request = self.context.get('request', None)
        if request is not None:
            return request.build_absolute_uri(url)

        return url

有谁知道如何在我的缩略图上获得正确的命名?

谢谢!

根据库的文档,Link。有 4 个可用的名称。

Four namers are included in easy_thumbnails:

``easy_thumbnails.namers.default``
    Descriptive filename containing the source and options like
    ``source.jpg.100x100_q80_crop_upscale.jpg``.

``easy_thumbnails.namers.hashed``
    Short hashed filename like ``1xedFtqllFo9.jpg``.

``easy_thumbnails.namers.alias``
    Filename based on ``THUMBNAIL_ALIASES`` dictionary key like ``source.jpg.medium_large.jpg``.

``easy_thumbnails.namers.source_hashed``
    Filename with source hashed, size, then options hashed like
    ``1xedFtqllFo9_100x100_QHCa6G1l.jpg``.

由于您没有设置它,它将采用默认选项,即 11.jpg.100x100_q85.jpg

但是您可以选择创建自定义命名器。 Link

To write a custom namer, always catch all other keyword arguments arguments
(with \*\*kwargs). You have access to the following arguments:
``thumbnailer``, ``source_filename``, ``thumbnail_extension`` (does *not*
include the ``'.'``), ``thumbnail_options``, ``prepared_options``.

也在这里Link

注意:有人也在 Github 上开了一个问题。 Link