Skip to content
Justice Tech School

ForeignKey

ForeignKey makes one to many links.

Example
class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    text = models.TextField()

c = Comment.objects.first()
print(c.post.title)

Exercise

On delete.

on_delete=models.

Quiz Test

What does CASCADE do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.