2011-07-22 34 views
0

我的模型时:Django的 - 方法调用在管理删除FK

class Spectacle(models.Model): 
    title = models.CharField(max_length=100) 
    slug = models.SlugField(max_length=100 unique=False) 
    description = models.TextField() 

class SpectacleGallery(models.Model): 
    spectacle = models.ForeignKey(Spectacle) 
    image = models.ImageField(upload_to=upload_path_handler, max_length=255, help_text=_(u'tylko pliki z rozszerzeniem .jpg')) 
    image_thumb = models.CharField(max_length=255, editable=False, blank=True, null=True) 

    def delete(self, *args, **kwargs): 
     image_thumb_abs = settings.MEDIA_ROOT + str(self.image_thumb) 
     # try to delete thumb and image 
     try: 
      remove(image_thumb_abs) 
     except Exception: 
      pass 

     try: 
      self.image.delete() 
     except Exception: 
      pass 

     super(SpectacleGallery, self).delete(*args, **kwargs) # Call the "real" delete() method. 

     #try to delete dir 
     try: 
      removedirs(settings.SPECTACLE_GALLERY_UPLOAD_PATH + str(self.spectacle_id)) 
     except: 
      pass 

现在管理员,当我从SpectacleGallery文件删除从磁盘和DB删除正常。问题是当我想删除眼镜。删除页面询问我是否确定删除眼镜和所有相关图像。我确认并且:删除数据库中的所有记录。不幸的是,SpectacleGallery中的文件不会被删除。我将其打印到SpectacleGallery中的delete()方法中。没有显示,所以我的问题是: 为什么删除方法不被调用时,以这种方式删除它?

回答