我有一个模型 - 产品,其中包含缩略图.我有另一个模型,其中包含与产品相关的图像 - 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',)
Run Code Online (Sandbox Code Playgroud)
以下删除方法(在产品类中)正在运行,但我更改了我的代码并且它不再有效 - 从我所读到的最佳做法是使用post_delete,而不是覆盖delete()
def delete(self):
images = ProductImage.objects.filter(product=self)
if images:
for image in images:
image.delete()
super(Product, self).delete()
Run Code Online (Sandbox Code Playgroud)
如何重写一个能实现我想要的删除方法?我曾尝试使用post_delete但到目前为止我一直没有成功,因为我不确定如何在删除ProductImage实例时应用它...