Relationships: ForeignKey
A ForeignKey links many rows to one row, such as many comments for one post. on_delete says what happens when the parent is removed. related_name names the reverse lookup.
Example
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name="comments")
text = models.TextField()
post.comments.all()
Comment.objects.filter(post__title="Hello")Exercise
Delete children with parent.
on_delete=models.Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.