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)
谢谢..似乎很好地工作 – Mike 2010-04-30 20:34:41