Search⌘ K

Many To One Relationship

Explore how to implement many-to-one relationships in Django using the ForeignKey field. Learn to link models like Company and Employee, understand the on_delete behavior, and see these relationships in the Django admin interface.

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 ...