on_delete is the ForeignKey rule for what happens to related objects when the referenced object is deleted. It makes the deletion policy explicit instead of leaving dangling relationships to surprise you later. section 4.7.1
Common policies include:
- CASCADE deletes associated objects too. A Room with venue = models.ForeignKey(Venue, on_delete=models.CASCADE) disappears when its Venue is deleted. section 4.7.1
- SET_NULL changes the relationship field to NULL instead, but the field must permit database NULL values with null=True. section 4.7.1
- Other choices can invoke different behavior, including a function, so select the policy that matches the relationship’s meaning. section 4.7.1
A venue with rooms
Venue.objects.create(name="CBGB") can own Room objects such as Red and Blue. With CASCADE, deleting CBGB also deletes those rooms; the reverse relationship is available through cbgb.room_set.all().
