Model Relationships (Django)
Django models relate to each other in three ways: ForeignKey (many-to-one), ManyToManyField (many-to-many), and OneToOneField (one-to-one). These fields let you navigate between related objects in Python without writing JOINs.
Django
Relationships express how your data connects. Many-to-one (ForeignKey) is for ownership like post→author; many-to-many (ManyToManyField) for shared associations like posts↔tags; one-to-one (OneToOneField) for extending a model, such as a UserProfile attached to each User.
Django generates both a forward accessor (post.author) and a reverse one (author.post_set, or your related_name). In queries you traverse relations with double underscores: Post.objects.filter(author__country='US') joins through to the author's table.
Choosing the right field and a good on_delete policy is a core modeling decision. Combine with select_related/prefetch_related when reading related data in bulk to keep query counts low.
Syntax
ForeignKey | ManyToManyField | OneToOneFieldExamples
Python
class Profile(models.Model):
user = models.OneToOneField("auth.User", on_delete=models.CASCADE)
bio = models.TextField(blank=True)
# one-to-one: user.profile and profile.user
# traverse in a query:
Post.objects.filter(author__profile__bio__icontains="python")