'How to delete files from filesystem using post_delete - Django 1.8
I have a model - Product, which contains a thumbnail image. I have another model which contains images associated with the product - ProductImage. I want to delete both the thumbnail and the images from the server when the product instance is deleted, and for a while this seemed to worked, but not anymore.
Relevant code...
Class Product(models.Model):
title = Charfield
thumbnail = ImageField(upload_to='thumbnails/', verbose_name='thumbnail', blank=True, )
Class ProductImage(models.Model):
product = models.ForeignKey(plant, default=None, related_name='images')
image = models.ImageField(upload_to='images/', verbose_name='image',)
The following delete method (in the product class) was working, but I changed my code and it no longer works - and from what i have read it is best practice to use post_delete, rather then override delete()
def delete(self):
images = ProductImage.objects.filter(product=self)
if images:
for image in images:
image.delete()
super(Product, self).delete()
How can I rewrite a delete method which will achieve what I want? I have tried to use post_delete but so far I have been unsuccessful because I am not sure how to apply it when it comes to deleting the ProductImage instance...
Solution 1:[1]
In 1.11 Django. Code work!
import os
from django.db import models
from django.utils import timezone
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver
class Post(models.Model):
category = models.ForeignKey(Category, verbose_name='?????????')
title = models.CharField('?????????', max_length=200, unique=True)
url = models.CharField('???', max_length=200, unique=True)
photo = models.ImageField('???????????', upload_to="blog/images/%Y/%m/%d/", default='', blank=True)
content = models.TextField('???????')
created_date = models.DateTimeField('????', default=timezone.now)
def _delete_file(path):
# Deletes file from filesystem.
if os.path.isfile(path):
os.remove(path)
@receiver(pre_delete, sender=Post)
def delete_img_pre_delete_post(sender, instance, *args, **kwargs):
if instance.photo:
_delete_file(instance.photo.path)
Solution 2:[2]
To effectively delete files from your system try:
files = modelName.objects.all().get(columnName=criteria)
files.columnNameFile.delete() files.columnNameFile2.delete() . . .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Сергей Зеленчук |
| Solution 2 | Ulises Ramírez |
