To make a One-To-One relationship, Django uses OneToOneField
. Just like in ForeignKey
, there are two key arguments you need to specify.
In addition to these arguments, you may need to add the primary_key option for the field.
In this practice, we'll explain how to use OneToOneField. In this case example, we'll create a new model PersonalInfo and connect it with the Employee model using One-To-One relationship.
Create the PersonalInfo model in the models.py file with OneToOneField
to connect with the Employee model.
Add the model after the Employee model, as the PersonalInfo model refers to the Employee model. Below is the code for the PersonalInfo model.
The yellow lines below are the new code.
:
class Employee(models.Model):
:
class PersonalInfo(models.Model):
name=models.OneToOneField(Employee, on_delete=models.CASCADE, primary_key=True)
tel=models.CharField(max_length=15)
address=models.CharField(max_length=50)
You need to add the new model in the admin.py file to reflect the model.
from .models import Employee
from .models import Division
from .models import PersonalInfo
admin.site.register(Employee)
admin.site.register(Division)
admin.site.register(PersonalInfo)
Run the three usual commands and go to the Django admin site.
python manage.py makemigrations employee_learning
python manage.py migrate
python manage.py runserver
You can see that the PersonalInfo model is added and connected with the Employee model.
As we set the primary key for the OneToOne relationship field, you'll encounter an error message when you try to register personal info for the same employee twice.
To make a One-To-One relationship, Django uses OneToOneField
. Just like in ForeignKey
, there are two key arguments you need to specify.
In addition to these arguments, you may need to add the primary_key option for the field.
In this practice, we'll explain how to use OneToOneField. In this case example, we'll create a new model PersonalInfo and connect it with the Employee model using One-To-One relationship.
Create the PersonalInfo model in the models.py file with OneToOneField
to connect with the Employee model.
Add the model after the Employee model, as the PersonalInfo model refers to the Employee model. Below is the code for the PersonalInfo model.
The yellow lines below are the new code.
:
class Employee(models.Model):
:
class PersonalInfo(models.Model):
name=models.OneToOneField(Employee, on_delete=models.CASCADE, primary_key=True)
tel=models.CharField(max_length=15)
address=models.CharField(max_length=50)
You need to add the new model in the admin.py file to reflect the model.
from .models import Employee
from .models import Division
from .models import PersonalInfo
admin.site.register(Employee)
admin.site.register(Division)
admin.site.register(PersonalInfo)
Run the three usual commands and go to the Django admin site.
python manage.py makemigrations employee_learning
python manage.py migrate
python manage.py runserver
You can see that the PersonalInfo model is added and connected with the Employee model.
As we set the primary key for the OneToOne relationship field, you'll encounter an error message when you try to register personal info for the same employee twice.