How to use django.core.files.storage.get_available_name? -
i storing files , need unique file name. found a suggestion use django.core.files.storage.get_available_name missing something. tried this:
class mymodel(models.model): name = models.charfield( max_length=200, null=true ) filecontent = models.filefield( null=true, upload_to=storage.get_available_name(name) )
and got error: typeerror: unbound method get_available_name() must called storage instance first argument (got charfield instance instead)
interpret meaning trying run method on class need storage-instance run on. there exist called defaultstorage supposed something "provides lazy access current default storage system" 1 not have get_available_name
method method _setup
can't called on class either. how supposed instance work on here?
the upload_to
argument directory path want upload files to. argument storage named storage
so should if want change default django's storage implementation use example
filecontent = models.filefield(null=true, upload_to='path/to/dir', storage=filesystemstorage())
if need implement own way play filenames have create class inherits storage
, overwrite it's get_available_name
method. example:
class myownstorage(filesystemstorage): def get_available_name(self, name): if self.exists(name): #return else: #return something_else
and use follows:
filecontent = models.filefield(null=true, upload_to='path/to/dir', storage=myownstorage())
Comments
Post a Comment