Django delete FileField
09:45 16 Apr 2013

I'm building a web app in Django. I have a model that uploads a file, but I can not delete the file. Here is my code:

class Song(models.Model):
    name = models.CharField(blank=True, max_length=100)
    author = models.ForeignKey(User, to_field='id', related_name="id_user2")
    song = models.FileField(upload_to='/songs/')
    image = models.ImageField(upload_to='/pictures/', blank=True)
    date_upload = models.DateField(auto_now_add=True)

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

Then, in python manage.py shell I do this:

song = Song.objects.get(pk=1)
song.delete()

It deletes the record from the database but not the file on server. What else can I try?

Thanks!

django django-models