Django Models with Relationships — One to One Relationship

3 min readAug 30, 2020


Hello all. I this blog I am explaining what is a One-to-One Relationship and how to perform it in Django.

Cover Image. Ignore it.

In my previous blog, I have explained what is relationships and the types of it. Do visit it.

What is a relationship and What is a one-to-one relationship?

As said, a relationship is an association between two entities. A relationship allows us to spit and store the data in different databases. The relationship works between two Relational Database Models.

A One-to-One relationship is a type of Relationship where both tables can have only one record on either side. A one-to-one relationship is like a relationship between a husband and a wife.

Now let’s see how does the relationship works with Django. Let’s start.

  1. Create a Django Project and Adhar app

Now let’s create our Django project. The below commands create a Django project and migrate the changes to the database.

$ django-admin startproject djRelation
$ cd djRelation/
$ python3 migrate
$ python3 runserver

Once done, check if the server is working well. Then just create a Django app within our Django Project where we will be creating our model.

$ python3 startapp adhar

Once done, do all the necessary steps for registering our app to our Django project as I did below.


# Application definition
'adhar', # add this line

Create a file in our app adhar and make sure the file should look like this.

from django.conf.urls import url, include
from django.urls import path
urlpatterns = []

To demonstrate this example, we need no URL. We can perform it in the Django shell itself. So I am not adding any URL here. After creating this file, just map the adhar/ to the djRelation/ file.


from django.conf.urls import url, include
from django.urls import path
from django.contrib import admin
urlpatterns = [

2. Crafting our Ahdar Model

I am creating two models, Person and Adhar where the primary key (id)of the model Person will be an OneToOneField of Adhar.


from django.db import models# Create your models here.
class Person(models.Model):
name = models.TextField(max_length=100)
email = models.EmailField()
mobile = models.TextField(max_length=100)
class Adhar(models.Model):
person = models.OneToOneField("Person",on_delete=models.CASCADE)
signature = models.TextField()
adhar_no = models.TextField(max_length=100)

Now just migrate.

$ python3 makemigrations
$ python3 migrate

3. Adding values to the Model

Let’s start our shell now. Now I am inserting some value to both tables. While inserting data to the Adhar table, make sure the person field should be an instance of thePerson table. Otherwise, it will through error.

$ python3 shellIn [1]: from adhar.models import PersonIn [2]: p1 = Person(name="Joan louji", email="", mobile="123412")
In [3]:
In [4]: p2 = Person(name="Rion", email="", mobile="0987632")
In [5]:
In [6]: from adhar.models import AdharIn [7]: a1 = Adhar(person=p1, signature="asdf234dsafuiq3&^$^GUJHVR", adhar_no="432198176354")
In [8]:
In [9]: a2 = Adhar(person=p2, signature="adfpoiwruywqtrgmnb13241)*&^%$", adhar_no="2314928376")
In [10]:
In [11]: p = Person.objects.all()
In [12]: print(p)
<QuerySet [<Person: Person object (1)>, <Person: Person object (2)>]>In [13]: a = Adhar.objects.all()
In [14]: print(a)
<QuerySet [<Adhar: Adhar object (1)>, <Adhar: Adhar object (2)>]>In [15]: a.values()Out[16]: <QuerySet [{'id': 1, 'person_id': 1, 'signature': 'asdf234dsafuiq3&^$^GUJHVR', 'adhar_no': '432198176354'}, {'id': 2, 'person_id': 2, 'signature': 'adfpoiwruywqtrgmnb13241)*&^%$', 'adhar_no': '2314928376'}]>

Now I am trying to add another Adhar data where I am be trying to use the Person instance p1 which has been already related to another Adhar.

This will through Integrity Error.

In [17]: a3 = Adhar(person = p1, signature = "1234lkjhjgfjgk*(&^%$", adhar_no="1234124")
In [18]:
IntegrityError: UNIQUE constraint failed: adhar_adhar.person_id

In my previous blog, I have explained the basics of Relationship and its types.

In my next blog, I am explaining how to perform many-to-one relationships with Django. Stay connected.

Feel free to contact me for any queries.



Complete Code can be found on my Github:

Happy coding…




Software Engineer at @Pando. Developer | Writer. From ABC to the world of code.