0

I was reading Django's article on model fields and models.CASCADE. I don't understand this phrase:

Model.delete() isn’t called on related models

models.CASCADE means that related objects to delete when the target object deleted ,so what is the meaning of "isn't called on related models"?

Please explain that for me.

rahnama7m
  • 865
  • 10
  • 38

1 Answers1

1

If your read the part after its maybe more clearly but the pre_delete and post_delete signals are sent for all deleted objects. So what this means is Django will delete the objects but won't call the delete method of those objects -and delete them by delete_batch method-. It will only fire the pre_delete and post_delete signals for that object.

rahnama7m
  • 865
  • 10
  • 38
Krukas
  • 657
  • 3
  • 10
  • So what happened if Django call the delete method of those objects? Does n't deleting those object meaning of calling `delete()` method of those objects? @krukas – rahnama7m Jan 03 '20 at 14:43
  • It doesn't call the delete for the related objects, but it will delete them. But what it will do is call the pre en post delete signals for those related objects. So if you have some logic that needs to be run on delete for a related object, you must use the pre or post delete signal because the delete function is not called. – Krukas Jan 03 '20 at 14:48
  • You couldn't explain differences between `Deleting objects` and `call delete() for them`. @krukas – rahnama7m Jan 03 '20 at 14:56
  • 1
    I don't really understand what you mean what the differences is, are we still talking about the delete of related objects with CASCADE? In the case of related objects that are deleted, Django deletes them directly within the ORM. And therefore the delete method on the model is not called. – Krukas Jan 03 '20 at 15:10