子类化sorl-thumbnail`ThumbnailBackend`类,并覆盖_get_thumbnail_filename不起作用

时间:2013-06-24 23:12:50

标签: django inheritance sorl-thumbnail method-overriding

我打算从ThumbnailBackend继承sorl.thumbnail.base类。我需要做的是覆盖_get_thumbnail_filename方法,将一些东西添加到原始(父)方法生成的文件名中。为此,我写了这样的话:

from sorl.thumbnail.base import ThumbnailBackend

class MyThumbnailBackend(ThumbnailBackend):
    def _get_thumbnail_filename(self, source, geometry_string, options):
        oldpath = super(ThumbnailBackend,self)._get_thumbnail_filename(source, geometry_string, options)
        oldpathlist = oldpath.split('/')
        # get the last item of 'oldpathlist' and
        # sufix it with useful info...
        # join the items with the modified one...
        return newpath

python继承应该有一些我缺少的东西,因为我一直收到以下错误:

AttributeError at /location/of/the/caller/class/
'super' object has no attribute '_get_thumbnail_filename'

如果我是对的,我在第一行导入this classfrom sorl.thumbnail.base import ThumbnailBackend肯定有_get_thumbnail_filename方法。

我做错了什么?

非常感谢!

1 个答案:

答案 0 :(得分:2)

您必须使用当前课程调用super,将super(ThumbnailBackend, self)更改为super(MyThumbnailBackend, self),就像这样

class MyThumbnailBackend(ThumbnailBackend):
    def _get_thumbnail_filename(self, source, geometry_string, options):
        return super(MyThumbnailBackend, self)._get_thumbnail_filename(source, geometry_string, options)