python - Accessing model from FileSystemStorage -


i have custom filesystemstorage.
idea pass optional filename parameter.
custome storge code:

class salesmapfilestores(filesystemstorage):     def __init__(self, location=none, base_url=none, filename=none):         if filename:             self.filename = filename         super(salesmapfilestores, self).__init__(location=location, base_url=base_url)      def get_available_name(self, name):         return name      def get_valid_name(self, name):         if self.filename:             return self.filename         return name      def _save(self, name, content):         if self.exists(name):             self.delete(name)         return super(salesmapfilestores, self)._save(name, conten 

what whant pass filename parameter model.
somethin this:

class salesmapimage(models.model):     name = models.charfield(max_length=254, verbose_name='filename')     image = salesmapimagefield(upload_to='salesmap/test', storage=salesmapfilestores(filename=name), verbose_name='test image',                                content_types=('image/jpeg', 'image/png')) 

but in case, django passes parameter model.charfield (it's obvious :)).
question is: how can access model instance storage?

in advance!

well, bit crazy idea can try override assignment field in class salesmapfilestores instance keep in sync name field this:

class salesmapimage(models.model):     name = models.charfield(max_length=254, verbose_name='filename')     image = salesmapimagefield(upload_to='salesmap/test', storage=salesmapfilestores(), verbose_name='test image',                                content_types=('image/jpeg', 'image/png'))      def __setattr__(self, key, value):         self.__dict__[key]=value         if key=='name':             self.image.storage.filename = value 

and general idea hook assignment of value update of filename field.

this supposing don't want update manually within view. because wouldn't take effort model_instance.storage.filename = self.name or add method custom storage class update filename.

update: heads storage=salesmapfilestores(). there passing instance of salesmapfilestores. not class, might possible you'll using same instace storing files , bring conflicts filename. can try this: storage=salesmapfilestores

hope helps!


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -