【发布时间】:2010-01-13 22:25:36
【问题描述】:
转到对象管理页面并选择删除时,以下内容非常有用。问题是,当使用多选并一次删除多个项目时,它不使用我的删除覆盖。我一直在寻找解决方案,但还没有找到,所以是时候求助于大师了 ;)
class Photo(models.Model):
name = models.CharField(max_length=256, unique=True)
slug = models.SlugField(unique=True)
image = models.ImageField(upload_to='photos/')
def delete(self):
super(Photo, self).delete()
### Check if the dir is empty, then remove the folder
ph = Photo.objects.filter(album=self.album)
if ph.count() == 0:
rmtree(os.path.join(settings.MEDIA_ROOT, 'photos/' + self.album.slug))
【问题讨论】:
-
这段代码有异味。我建议不要使用它。
-
@Ignacio - 请扩展
-
在多处理情况下,一个连接可能会删除相册中的最后一张照片,而另一个连接正在将照片上传到该相册。由于没有看到创建目录的代码,因此不可能对这段代码的活跃程度有深刻的理解,但我不会冒险在我的服务器上运行。
-
之前有人问过这个问题:stackoverflow.com/questions/1471909/…