Django removes models and cancels the delete method

I have 2 models

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()

If I do the following

>>> 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)
[]

As you can see, there is an application-related object associated with vhost, but when I delete vhost, the object installed by the application is gone, but printing is never called.

Any easy way to do this without repeating objects in removing vhost?

Decision

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

    print "need to delete apps"


pre_delete.connect(delete_apps, sender=ApplicationInstalled)
+3
source share
1 answer

Since django received signals, I have found that I almost never need to undo save / delete.

Whatever you do, most likely it will be possible to execute in pre_delete or post_delete .

, pre_delte.

+5

Source: https://habr.com/ru/post/1743563/


All Articles