Django Many To One Relations

Srivastava
3 min readSep 30, 2021

Want to learn about Django Models Many To One Relationship 🤔…? Here is the complete guide for you😌.

Read about One-To-One relationship of Django Model.

What is Many-To-One Relationship in Models

As the name suggest Many-To-One Relationship refers to a relationship between one or more entities where a single entity can have many entities connected.

For Example:-

  • Contacts of a User
  • Blog’s of a User/Author
  • Mother — Children

Let’s create two models User and Contacts

Note:

Don’t get confused User model we had created with that of Django User Model (from django.contrib.auth) .

After creating models don’t forget to migrate them.

To do so enter the following commands in the terminal.

python manage.py makemigrations
python manage.py migrate

Now let’s test for, the one to one relations between our models.

Add some values to both the tables, either by admin panel or by using Django shell

Added values from Django Admin Panel

For adding values to tables using Django shell, run following command in terminal

python manage.py shell

You’re now in Django’s interactive console. It’s just like the Python prompt, but with some additional Django features. All the Python commands can be run here .

As here we can see in the Contact QuerySet, or in the admin panel inside Contact Model, User’s can have multiple Email Id’s.

User with multiple Email Id’s

Hence our models (Contact And User) have the relationship of Many-To-One .

I had used the ModelAdmin class for the representation of model in the admin interface.

ForeignKey

To define a many-to-one relation between models tables ForeignKey Field type is used. It requires positional argument, the class to which model is related.

syntax:

ForeignKey(to, on_delete, **options)

  1. to — The class to which model is related. Likewise in our case(User)
  2. on_delete — It is simply used to delete the table value or column value if the parent/child table is deleted.

Mainly used values for on_delete in many_to_one relation are

  • CASCADE — Deletes the object containing ForeginKey (in our case if User value is deleted its respective Contacts(email) value will also be deleted from the database)
  • PROTECT — Prevents the deletion of the referenced object by raising ProtectedError, that is User’s value cannot be deleted if it has a single contact left.
  • SET_NULL — Sets the ForeignKey value to null. If User value is deleted its Contacts will be saved in database with foreignkey value as null.

Note:- when using SET_NULL in on_delete, we have to pass ‘null’ as TRUE, which is FALSE by default.

class Contact(models.Model):
----
user=models.ForeignKey(to=User,on_delete=models.SET_NULL, null=TRUE)

There are other values also, like

  • SET_DEFAULT — It sets the foreign key to it its default value as defined in the model. Default needs to defined beforehand.
  • SET() — It is used set the value to something of your choice when its related entity is deleted.
  • DO_NOTHING — It is used to delete the entity from admin view but does not effect the database.

--

--