#4339: Override an existing file, using Model.save_FIELD_file method,
---------------------------------------------------+------------------------
          Reporter:  elaat...@gmail.com            |         Owner:  Gulopine   
            
            Status:  new                           |     Milestone:             
            
         Component:  Database layer (models, ORM)  |       Version:  SVN        
            
        Resolution:                                |      Keywords:  FileField 
db fs-rf-docs
             Stage:  Design decision needed        |     Has_patch:  1          
            
        Needs_docs:  1                             |   Needs_tests:  0          
            
Needs_better_patch:  1                             |  
---------------------------------------------------+------------------------
Comment (by nwelch):

 Overriding both _save and get_available_name seems to do the trick for me.
 It *seems* that it should be unnecessary to override both, but overwriting
 _save wasn't enough, and only overriding get_available_name resulted in an
 infinite loop since _save demands that the file not exist (i.e. it won't
 do an actual overwrite -- but rather a delete, then a write).

 {{{
 from django.core.files.storage import FileSystemStorage
 class OverwriteStorage(FileSystemStorage):
     def _save(self, name, content):
         if self.exists(name):
             self.delete(name)
         return super(OverwriteStorage, self)._save(name, content)
     def get_available_name(self, name):
         return name
 }}}

 I disclaim any copyright to the above code.  (I'm guessing it would be
 considered too trivial to be copyrighted anyway, but just in case)

 Also, while this is maybe a bit more likely to cause race conditions,
 nothing particularly harmful happens as a result.  The thread will just
 have to try again until it succeeds.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/4339#comment:25>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to