[Answer]-Django admin delete != Model.delete()

1๐Ÿ‘

โœ…

I think youโ€™ve misunderstood how cascade works. If I understand your question correctly, your models are something like:

class P(models.Model):
    pass

class T(models.Model):
    # on_delete=models.CASCADE is the default, including it here to be explicit
    p = models.ForeignKey(P, on_delete=models.CASCADE) 

Deleting a t will not cause the related p to be deleted. The CASCADE means that if you delete a p, then all tโ€˜s that link to p will be deleted.

p = P.objects.get(name="x")
p.delete() # all t's that link to p will be deleted.

This is the case whether you use the Django admin or do it programmatically.

๐Ÿ‘คAlasdair

Leave a comment