Y
Y
yeputons2011-09-28 21:01:19
Django
yeputons, 2011-09-28 21:01:19

Django - how to configure the removal of unnecessary files (from FileField) when deleting / changing the model?

Good evening.
There is a model, it has several fields of the FileField and ImageField type.
Is there any documented and short way to make it so that:
a) When a model is deleted, all files associated with it are automatically deleted
b) When a model is changed, old versions of files are automatically deleted if new ones are loaded (or, accordingly, nothing changes).
Both points are solved by a snippet with post_delete and pre_save event handling. But in it I make an additional call to the database to find out the location of the old file.
Of the other "options" there is an extension of the standard descriptor to save the path to the old file directly in the model. But this is already more complicated than a single query to the database.
There are two questions:
1) is it possible to solve points a) and b) with built-in tools
2) what other beautiful solutions do you see?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
homm, 2011-09-28
@yeputons

First you need to say why this behavior was introduced at all. The fact is that deleting an object (by the way, you mistakenly call objects models) does not mean that it is gone and will not return. Deletion may well occur in a transaction that will then be rolled back. The developers of Janga correctly identified that this is not the task of running the application, but of administering it. Therefore, the best solution would be to create a management action for your application that will be triggered by cron. In it, you can select all real-life files and copy them to a separate location, and then change this location with the folder where the files are uploaded. If the files are stored on the local machine, it is better to create symlinks, save both time and disk space.

U
un1t, 2012-08-18
@un1t

I don't know if the problem is still relevant, but for me it has been such all this time. As a result, I wrote an application that deals with deleting old files. github.com/un1t/django-cleanup

S
Sergey Eremin, 2020-05-16
@Sergei_Erjemin

It was advised django-cleanuphere - this is a really good solution, but when in some models it is necessary to delete, but in others it is not necessary, or even different behavior with files in different places (delete files somewhere, but leave them somewhere), that is another solution:
Let's say there is a model:

class tb_icons(models.Model):
    image = models.ImageField(max_length=128, verbose_name=u"картинка")
    # ...
    # ...

If you want to delete a record from the table tb_iconsand the accompanying file from imageit, you do this (for example, to delete the first record):
tb_icons.objects.get(id=1).image.delete(save=True)
tb_icons.objects.get(id=1).delete()

Accordingly, if save=Falsethe file will not be deleted.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question