ImageField not deleted while changing

41 views
Skip to first unread message

galago

unread,
Feb 26, 2011, 4:12:14 PM2/26/11
to django...@googlegroups.com
Why old file is not deleted if I want to change it? I select new file in form and it's saved but old file is not deleted. Is it my fault or Django is designed not to delete it (if so, how to delete old file)?

Borislav Petrović

unread,
Feb 27, 2011, 8:05:28 AM2/27/11
to django...@googlegroups.com
As far as I know, you should implement the file deletion by yourself. You should code it on the Models delete function. preserving the default behaviour.
here is a link to some read: http://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

How you do the file deletion is just pure python code further on.

Artur Wdowiarski

unread,
Feb 27, 2011, 8:36:12 AM2/27/11
to django...@googlegroups.com, Borislav Petrović
Borislav is right, although it seems to me that overriding the model's save method would be more appropriate in Your case.

On the other hand, You really should think twice before doing so. There's actually a good reason for django orm not to delete the actual file after the corresponding FileField had been changed or deleted. Your models' edition/deletion is usually handled in db transactions that are rolled back when something goes wrong and unfortunately there's no way to undo a file's deletion...

Perhaps it's better to periodically run a script that deletes orphaned files.

2011/2/27 Borislav Petrović <chu...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages