2011-07-22 33 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()方法中。沒有顯示,所以我的問題是: 爲什麼刪除方法不被調用時,以這種方式刪除它?

回答