2010-04-30 95 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