0

In the Django documentation, it mentions that when you directly specify the new related set, any models no longer in the set are simply disassociated instead of deleted.

In other words, if you have a situation like this:

#author.books contains [book1, book2, book3]
author.books = [book1, book2]
author.save()

The book 3 model is no longer associated with the author, but it is NOT deleted. Is there a way to disassociate AND delete the model?

Leah Sapan
  • 3,621
  • 7
  • 33
  • 57

1 Answers1

0

If book3 is an model object, you would just run

book3.delete()

And it will disassociate it from the Author's books (if that is a Key or many-to-many key for example) because it is deleted. And the record is also deleted from the Book model.

Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • Well yes, but if I am being passed a new array of items to save, then the logic becomes a little overly verbose because I have to check for items no longer in the array. There's no automatic way? – Leah Sapan Aug 28 '14 at 00:00
  • Do you want to delete all books that don't belong to any author? You can override the `save()` method in the `model`, but you would need some logic as to which books to delete. – Aaron Lelevier Aug 28 '14 at 00:09