Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Cascade deletion in ManyToManyRelation

Using the following related models (one blog entry can have multiple revisions):

class BlogEntryRevision(models.Model):     revisionNumber = models.IntegerField()     title = models.CharField(max_length = 120)     text = models.TextField()     [...]  class BlogEntry(models.Model):     revisions = models.ManyToManyField(BlogEntryRevision)     [...] 

How can I tell Django to delete all related BlogEntryRevisions when the corresponding BlogEntry is deleted? The default seems to be to keep objects in a many-to-many relation if an object of the "other" side is deleted. Any way to do this - preferably without overriding BlogEntry.delete?

like image 732
AndiDog Avatar asked Oct 14 '10 20:10

AndiDog


2 Answers

I think you are misunderstanding the nature of a ManyToMany relationship. You talk about "the corresponding BlogEntry" being deleted. But the whole point of a ManyToMany is that each BlogEntryRevision has multiple BlogEntries related to it. (And, of course, each BlogEntry has multiple BlogEntryRevisions, but you know that already.)

From the names you have used, and the fact that you want this deletion cascade functionality, I think you would be better off with a standard ForeignKey from BlogEntryRevision to BlogEntry. As long as you don't set null=True on that ForeignKey, deletions will cascade - when the BlogEntry is deleted, all Revisions will be too.

As Of Django 2.0

The ForeignKey initializer now requires you to specify the on_delete parameter:

from django.db import models from .models import MyRelatedModel   class model(models.Model):     related_model = models.ForeignKey(MyRelatedModel, on_delete=models.CASCADE) 
like image 111
Daniel Roseman Avatar answered Sep 20 '22 20:09

Daniel Roseman


I had this exact use-case today:

  • Model Author: can have several entries
  • Model Entry: can have several authors

For this, I'm using a ManyToManyRelationship.

My use-case was: if I delete the last entry of a particular author, then this author should be deleted as well.

The solution can be achieved using the pre_delete signal:

@receiver(pre_delete, sender=Entry) def pre_delete_story(sender, instance, **kwargs):     for author in instance.authors.all():         if author.entries.count() == 1 and instance in author.entries.all():             # instance is the only Entry authored by this Author, so delete it             author.delete() 
like image 34
jessepeng Avatar answered Sep 17 '22 20:09

jessepeng