我打算从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 class:from sorl.thumbnail.base import ThumbnailBackend
肯定有_get_thumbnail_filename
方法。
我做错了什么?
非常感谢!
答案 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)