2010-04-30 75 views
3

我有2种型号Django的删除模型和压倒一切的删除方法

class Vhost(models.Model): 
    dns = models.ForeignKey(DNS) 
    user = models.ForeignKey(User) 
    extra = models.TextField() 


class ApplicationInstalled(models.Model): 
    user = models.ForeignKey(User) 
    added = models.DateTimeField(auto_now_add=True) 
    app = models.ForeignKey(Application) 
    ver = models.ForeignKey(ApplicationVersion) 
    vhost = models.ForeignKey(Vhost) 
    path = models.CharField(max_length=100, default="/") 


    def delete(self): 

     # 
     # remove the files 
     # 
     print "need to remove some files" 


     super(ApplicationInstalled, self).delete() 

如果我做了以下

>>> vhost = Vhost.objects.get(id=10) 
>>> vhost.id 
10L 
>>> ApplicationInstalled.objects.filter(vhost=vhost) 
[<ApplicationInstalled: http://wiki.jy.com/>] 
>>> vhost.delete() 
>>> ApplicationInstalled.objects.filter(vhost=vhost) 
[] 

正如你可以看到有链接到虚拟主机的applicationinstalled对象,但是当我删除vhost,应用程序安装的对象消失了,但打印从未被调用。

任何简单的方法来做到这一点,而无需遍历虚拟主机删除中的对象?

解决方案

def delete_apps(sender, **kwargs): 
    obj = kwargs['instance'] 

    print "need to delete apps" 


pre_delete.connect(delete_apps, sender=ApplicationInstalled) 

回答

5

自从Django的有signals我发现,我几乎从来没有需要重写保存/删除。

无论您需要做什么都可能在pre_deletepost_delete信号中完成。

在这种情况下,您似乎希望在pre_delte信号中输入delete in bulk

+0

谢谢..似乎很好地工作 – Mike 2010-04-30 20:34:41