Passing parameters to custom storage

Hi!

I’m using a custom storage for saving files to the cloud. I’d like the ability to pass in some additional information, so added a parameter to the class (a summary of the code is below). My question: is this a good idea, or is this likely to cause a problem with Django?

Model

class MyModel(models.Model):
    my_file = models.FileField(storage=MyStorage("some text"))

Storage

class MyStorage(Storage):
    def __init__(self, my_parameter):
        self.my_parameter = my_parameter

Thanks!
Tom

That’s a good approach, as taken by the storage classes in django-storages, for example S3Boto3Storage.

Make sure you call super() and pass arbitrary arguments on:

class MyStorage(Storage):
    def __init__(self, my_parameter, **kwargs):
        super().__init__(**kwargs)
        self.my_parameter = my_parameter
1 Like

That’s great, thanks Adam, especially for the example link.