django删除模型并覆盖删除方法

Mik*_*ike 3 django django-models

我有2个型号

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()
Run Code Online (Sandbox Code Playgroud)

如果我做以下事情

>>> 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)
[]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,有一个应用程序安装的对象链接到vhost但是当我删除vhost时,应用程序安装的对象已经消失但是打印永远不会被调用.

没有迭代vhost中的对象删除任何简单的方法吗?

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

    print "need to delete apps"


pre_delete.connect(delete_apps, sender=ApplicationInstalled)
Run Code Online (Sandbox Code Playgroud)

rz.*_*rz. 5

自从django得到信号后,我发现我几乎不需要覆盖保存/删除.

无论您需要做什么,都可以通过pre_deletepost_delete信号完成.

在这种情况下,您似乎希望在pre_delte信号中批量删除.