سؤال

I am trying to implement a new Storage class that subclasses the S3BotoStorage from Django-Storages. (http://code.larlet.fr/django-storages/src/83fa2f0ba20c/storages/backends/s3boto.py)

Whenever the save method of my new class is being called, I want to do some other stuff and then call the S3BotoStorage._save() method. Like this:

class CustomStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        super(CustomStorage, self).__init__(*args, **kwargs)

    def _save(self,*args, **kwargs):
        #Will do stuff there
        print >> sys.stderr, "%s" % (self.bucket)
        super(CustomStorage, self)._save(*args, **kwargs)

If I don't have this CustomStorage._save() method, everything works well (ie the S3BotoStorage._save(name,content) is called and everything uploads to S3). If I have this new CustomStorage.save method however, I get a 500 error. (It does get called though, as my error message appears in the terminal). I can't see any call stack or anything.

I tried:

def save(self,*args, **kwargs):
def save(self,name, content):

Neither of these also worked.

Any ideas?!

Thanks!

هل كانت مفيدة؟

المحلول

Storage._save should return the name of the file being saved. Your _save does not. You should return the value from the super call.

class CustomStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        super(CustomStorage, self).__init__(*args, **kwargs)

    def _save(self,*args, **kwargs):
        #Will do stuff there
        print >> sys.stderr, "%s" % (self.bucket)
        return super(CustomStorage, self)._save(*args, **kwargs)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top