I am using 1.2.5 with a standard ImageField and using the built-in storage backend. Files upload fine but when I remove an entry from admin the actual file on the server does not delete.
相关问题
- Django __str__ returned non-string (type NoneType)
- What is the best way to do a search in a large fil
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
相关文章
- TypeError: 'BaseQuery' object is not calla
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- What is the correct way to declare and use a FILE
- Django/Heroku: FATAL: too many connections for rol
- Making new files automatically executable?
- Django is sooo slow? errno 32 broken pipe? dcramer
You can receive the
pre_delete
orpost_delete
signal (see @toto_tico's comment below) and call the delete method on the FileField object, thus (in models.py):I may have a special case since I am using the upload_to option on my file field with dynamic directory names but the solution I found was to use os.rmdir.
In models:
...
This functionality will be removed in Django 1.3 so I wouldn't rely on it.
You could override the
delete
method of the model in question to delete the file before removing the entry from the database completely.Edit:
Here is a quick example.
Try django-cleanup
settings.py
Maybe it's a little late. But the easiest way for me is to use a post_save signal. Just to remember that signals are excecuted even during a QuerySet delete process, but the [model].delete() method is not excecuted during the QuerySet delete process, so it's not the best option to override it.
core/models.py:
core/signals.py
You may consider using a pre_delete or post_delete signal:
https://docs.djangoproject.com/en/dev/topics/signals/
Of course, the same reasons that FileField automatic deletion was removed also apply here. If you delete a file that is referenced somewhere else you will have problems.
In my case this seemed appropriate because I had a dedicated File model to manage all of my files.
Note: For some reason post_delete doesn't seem to work right. The file got deleted, but the database record stayed, which is completely the opposite of what I would expect, even under error conditions. pre_delete works fine though.