我有一个模型 - 产品,其中包含缩略图。我有另一个模型,其中包含与产品相关的图像 - ProductImage。 我希望在删除产品实例时从服务器中删除缩略图和图像,有一段时间这似乎有效,但现在不行了。
相关代码......
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',)
以下删除方法(在产品类中)正在运行,但我更改了我的代码并且它不再有效 - 从我所读到的最佳做法是使用post_delete,而不是覆盖delete()
def delete(self):
images = ProductImage.objects.filter(product=self)
if images:
for image in images:
image.delete()
super(Product, self).delete()
如何重写一个能实现我想要的删除方法?我曾尝试使用post_delete但到目前为止我一直没有成功,因为我不确定如何在删除ProductImage实例时应用它...
答案 0 :(得分:20)
以下是post_delete
:
import os
from django.db import models
def _delete_file(path):
""" Deletes file from filesystem. """
if os.path.isfile(path):
os.remove(path)
@receiver(models.signals.post_delete, sender=ProductImage)
def delete_file(sender, instance, *args, **kwargs):
""" Deletes image files on `post_delete` """
if instance.image:
_delete_file(instance.image.path)
@receiver(models.signals.post_delete, sender=Product)
def delete_file(sender, instance, *args, **kwargs):
""" Deletes thumbnail files on `post_delete` """
if instance.thumbnail:
_delete_file(instance.thumbnail.path)
覆盖delete()
方法:
class Product(models.Model):
...
def delete(self):
images = ProductImage.objects.filter(product=self)
for image in images:
image.delete()
self.thumbnail.delete()
super(Product, self).delete()
class ProductImage(models.Model):
...
def delete(self):
self.image.delete()
super(ProductImage, self).delete()
了解Cascade
删除:docs
答案 1 :(得分:2)
在1.11 Django中。代码工作!
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)
答案 2 :(得分:0)