问题描述
我有一个模型 - 产品,其中包含一个缩略图。我有另一个模型,其中包含与产品相关联的图像 - ProductImage。当产品实例被删除时,我想从服务器中删除缩略图和图像,一段时间似乎有效,但不再存在。
相关代码...
类产品(models.Model):
title = Charfield
thumbnail = ImageField(upload_to ='thumbnailails /',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 = self)
如果图像:
for im in im年龄:
image.delete()
超级(产品,自我).delete()
如何重写一个可以实现我想要的删除方法?我试图使用post_delete,但到目前为止,我一直不成功,因为我不知道如何应用它,当涉及到删除ProductImage实例...
这里是一个例子, post_delete
:
import os
from django.db import models
def _delete_file(path):
从文件系统中删除文件。
如果os .path.isfile(path):
os.remove(path)
@receiver(models.signals.post_delete,sender = ProductImage)
def delete_file(sender,instance, * args,** kwargs):
删除`post_delete`上的图像文件
如果instance.image:
_delete_file(instance.image.path)
@receiver(models.signals.post_delete,sender = Product)
def delete_file(sender,instance,* args,** kwargs):
删除`post_delete`上的缩略图文件
if instance.thumbnail:
_delete_file(instance.thumbnail.pa th)
覆盖 delete()
方法:
class Product(models.Model):
...
def delete (self):
images = ProductImage.objects.filter(product = self)
图像中的图像:
image.delete()
self.thumbnail.delete()
super(Product,self).delete()
class ProductImage(models.Model):
...
def delete (self):
self.image.delete()
super(ProductImage,self).delete()
阅读关于 Cascade
delete:
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...
And here's an example with the 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)
Overriding the delete()
method:
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()
Read about Cascade
delete: docs
这篇关于如何使用post_delete - Django 1.8从文件系统中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!