Many To One Relationship

Let's see how to make Many to One relationship between Django models.

What are foreign keys?

When we need to create relations between data, we use foreign keys. Foreign keys relate one table in the database to another table. Let’s see how this relationship is handled in Django models.

Foreignkey field

This field is used for a Many-to-One relationship. To understand how this relationship between models works, let’s create new models:

  • Company
  • Employee

So, every employee would have one company associated with it, and every company would have many employees. This relation is displayed in the following illustration:

Get hands-on with 1200+ tech skills courses.