Django - update a model won't delete the old FileField
Asked Answered
W

2

6

I am implementing an application with django, which has a model with a FileField:

class Slideshow(models.Model):
    name = models.CharField(max_length=30,unique=True)
    thumbnail = models.FileField(max_length=1000,upload_to="images/app/slideshows/thumbnails")

and I have an admin backend where django manages the models. I just added the file admin.py and django manages everything for me

from django.contrib import admin

from apps.gallery.models import Slideshow

admin.site.register(Slideshow)

In the backend, it is possible to add, delete and update the slideshows. However, when I try to update a slideshow and change its attribute thumbnail [FileField], django does not delete the old file. Consequently, after several updates the server is filled with many files which are useless. My question is: how can I make django delete those files automatically after an update?

I would really appreciate your help

Warrington answered 7/1, 2010 at 22:1 Comment(0)
D
1

I'm sure Django does this by design. It can't know, for example, whether any other models might be using that file. You would also be really surprised if you expected the file to remain and discovered that django deleted it!

However, there's also the issue that as soon as you change the file field, you lose the old file name.

There's an open ticket about that problem: http://code.djangoproject.com/ticket/11663

There's a patch in http://code.djangoproject.com/ticket/2983 which shows how to override __set__ to store the previous file name. Then your model's __save__ method can get access to the previous file name to delete it.

Drape answered 7/1, 2010 at 22:15 Comment(2)
Yes, you are right! The django developers seem working in a new design for this case in new releases. One of the options is to add an additional option in FileFields to make files replaceable on uploads: groups.google.com/group/django-developers/browse_thread/thread/… I support this idea, "Thank you for your help Seth" I might use that patch you told me about.Warrington
Just had another idea - you could try adding the show_hidden_initial option to your FileField. Presumably that would give you access to the original value of the FileField before a new file was uploaded.Drape
B
2

I thought much about this problem, and eventually I find out a solution than works well for me. You can find all models in project and connect pre_save and post_delete signals to them.

At the end I made app, which sloves this problem - django-cleanup

Bewray answered 17/8, 2012 at 12:27 Comment(1)
Looks like a great solution. I am not working on django anymore and can't test it but I Thank You for your contribution, un1t!Warrington
D
1

I'm sure Django does this by design. It can't know, for example, whether any other models might be using that file. You would also be really surprised if you expected the file to remain and discovered that django deleted it!

However, there's also the issue that as soon as you change the file field, you lose the old file name.

There's an open ticket about that problem: http://code.djangoproject.com/ticket/11663

There's a patch in http://code.djangoproject.com/ticket/2983 which shows how to override __set__ to store the previous file name. Then your model's __save__ method can get access to the previous file name to delete it.

Drape answered 7/1, 2010 at 22:15 Comment(2)
Yes, you are right! The django developers seem working in a new design for this case in new releases. One of the options is to add an additional option in FileFields to make files replaceable on uploads: groups.google.com/group/django-developers/browse_thread/thread/… I support this idea, "Thank you for your help Seth" I might use that patch you told me about.Warrington
Just had another idea - you could try adding the show_hidden_initial option to your FileField. Presumably that would give you access to the original value of the FileField before a new file was uploaded.Drape

© 2022 - 2024 — McMap. All rights reserved.