Login

File deletion of FileField and ImageField in the Admin panel

Author:
davmuz
Posted:
July 4, 2012
Language:
Python
Version:
1.4
Score:
2 (after 2 ratings)

This code runs well on Django 1.4 also with the Admin panel. It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class ImageModel(models.Model):
    image = ImageField(...)

    def delete(self, *args, **kwargs):
        # You have to prepare what you need before delete the model
        storage, path = self.image.storage, self.image.path
        # Delete the model before the file
        super(ImageModel, self).delete(*args, **kwargs)
        # Delete the file after the model
        storage.delete(path)

More like this

  1. Add Toggle Switch Widget to Django Forms by OgliariNatan 2 weeks, 1 day ago
  2. get_object_or_none by azwdevops 4 months, 1 week ago
  3. Mask sensitive data from logger by agusmakmun 6 months ago
  4. Template tag - list punctuation for a list of items by shapiromatron 1 year, 8 months ago
  5. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 8 months ago

Comments

Please login first before commenting.