What's the difference between django OneToOneField and ForeignKey?
Differences between OneToOneField(SomeModel)
and ForeignKey(SomeModel, unique=True)
as stated in The Definitive Guide to Django:
OneToOneField
A one-to-one relationship. Conceptually, this is similar to a
ForeignKey
withunique=True
, but the "reverse" side of the relation will directly return a single object.
In contrast to the OneToOneField
"reverse" relation, a ForeignKey
"reverse" relation returns a QuerySet
.
Example
For example, if we have the following two models (full model code below):
-
Car
model usesOneToOneField(Engine)
-
Car2
model usesForeignKey(Engine2, unique=True)
From within python manage.py shell
execute the following:
OneToOneField
Example
>>> from testapp.models import Car, Engine
>>> c = Car.objects.get(name='Audi')
>>> e = Engine.objects.get(name='Diesel')
>>> e.car
<Car: Audi>
ForeignKey
with unique=True
Example
>>> from testapp.models import Car2, Engine2
>>> c2 = Car2.objects.get(name='Mazda')
>>> e2 = Engine2.objects.get(name='Wankel')
>>> e2.car2_set.all()
[<Car2: Mazda>]
Model Code
from django.db import models
class Engine(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car(models.Model):
name = models.CharField(max_length=25)
engine = models.OneToOneField(Engine)
def __unicode__(self):
return self.name
class Engine2(models.Model):
name = models.CharField(max_length=25)
def __unicode__(self):
return self.name
class Car2(models.Model):
name = models.CharField(max_length=25)
engine = models.ForeignKey(Engine2, unique=True, on_delete=models.CASCADE)
def __unicode__(self):
return self.name
A ForeignKey
is a many-to-one relationship. So, a Car
object might have many instances of Wheel
. Each Wheel
would consequently have a ForeignKey
to the Car
it belongs to. A OneToOneField
would be like an instance of Engine
, where a Car
object can have one and only one.