A Complete Guide to One‑to‑One Relationships in Django Using OneToOneField

This article explains how to define and work with one‑to‑one relationships in Django using OneToOneField. It covers creating related objects, accessing relationships from both sides, reassigning one‑to‑one links, querying across relationships, deletion behavior with CASCADE, and working with related models such as Waiter. All concepts are demonstrated with practical Python API examples.

OneToOneField, one-to-one relationshipDjango ORM, CASCADEreverse relation, waiter_set

~2 min read · Updated Mar 10, 2026

1. Defining a One‑to‑One Relationship

A one‑to‑one relationship is defined using OneToOneField. In the example below, each Place may optionally have one Restaurant, and each Restaurant corresponds to exactly one Place.


class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

class Restaurant(models.Model):
    place = models.OneToOneField(
        Place,
        on_delete=models.CASCADE,
        primary_key=True,
    )
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

2. Creating Data

Creating Place instances:


p1 = Place(name="Demon Dogs", address="944 W. Fullerton"); p1.save()
p2 = Place(name="Ace Hardware", address="1013 N. Ashland"); p2.save()

Creating a Restaurant:

The Restaurant uses the Place as its primary key:


r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
r.save()

3. Accessing the Relationship

Restaurant → Place:


r.place

Place → Restaurant:


p1.restaurant

If a Place has no Restaurant:


hasattr(p2, "restaurant")  # False

4. Reassigning a One‑to‑One Relationship

You can assign a Restaurant to a different Place:


r.place = p2
r.save()
p2.restaurant  # Now belongs to p2

And assign it back from the reverse side:


p1.restaurant = r
p1.restaurant

Important: The related object must be saved first


p3 = Place(name="Demon Dogs", address="944 W. Fullerton")
Restaurant.objects.create(place=p3, ...)  # ValueError

5. Querying Across One‑to‑One Relationships

Restaurant → Place:


Restaurant.objects.get(place=p1)
Restaurant.objects.filter(place__name__startswith="Demon")
Restaurant.objects.exclude(place__address__contains="Ashland")

Place → Restaurant:


Place.objects.get(restaurant=r)
Place.objects.get(restaurant__place__name__startswith="Demon")

6. Deletion Behavior (CASCADE)

Deleting a Place deletes its Restaurant automatically:


p2.delete()
Restaurant.objects.all()  # Only the restaurant for p1 remains

7. Related Models: Waiter

In the example, Waiter has a ForeignKey to Restaurant:


w = r.waiter_set.create(name="Joe")

Querying Waiters:


Waiter.objects.filter(restaurant__place=p1)
Waiter.objects.filter(restaurant__place__name__startswith="Demon")

Conclusion

One‑to‑one relationships in Django are ideal when two models should form a strict pair. Using OneToOneField, you can easily define the relationship, navigate it from both sides, reassign linked objects, and perform expressive queries. Django’s reverse managers and lookup chaining make working with one‑to‑one relationships intuitive and powerful.

Written & researched by Dr. Shahin Siami

Related Articles

Django Tasks Framework: A Complete Guide to Background Task Execution in Django 6.0

Django 6.0 introduces the Tasks framework, a built‑in system for defining and queuing background work outside the request–response cycle. This article explains how Tasks work, how to configure backends, how to define and enqueue tasks, how context works, and how to integrate third‑party worker systems for production environments.

Continue

Asynchronous Support in Django: A Complete Guide to Async Views, ORM, Middleware, Performance, and Safety

This article explains Django’s asynchronous (async) capabilities, including async views, ASGI support, middleware behavior, async ORM features, performance considerations, handling disconnects, and Django’s async safety protections. It also covers how to use sync_to_async(), async ORM methods, and how to safely run synchronous code in async environments.

Continue

Django System Check Framework: A Complete Guide to Writing, Registering, Running, and Testing System Checks

This article explains Django’s System Check Framework—a powerful mechanism for detecting configuration issues, validating project structure, and ensuring code quality. It covers how checks are executed, how to write custom checks, how messages work, how to register and tag checks, how to extend checks for fields and models, and how to write both unit and integration tests for system checks.

Continue

Django Signals: A Complete Guide to Listening, Connecting, Sending, and Managing Application Events

This article provides a comprehensive explanation of Django’s signal system—an event‑driven mechanism that allows decoupled applications to react to actions occurring elsewhere in the framework. It covers how to define receivers, connect signals, use decorators, handle specific senders, organize signal code, and follow best practices to avoid complexity.

Continue

Understanding Django Settings: Configuration, Environment Management, and Best Practices

This article provides a complete overview of Django’s settings system. It explains how settings files work, how to designate a settings module, how to use settings in your code, how to configure Django manually, how to secure sensitive settings, and how to work with custom default settings. It also covers the role of django.setup() for standalone scripts.

Continue

Serializing and Deserializing Django Objects: A Complete Guide to Django’s Serialization Framework

This article explains Django’s serialization framework, including how to serialize and deserialize model instances, work with subsets of fields, handle inherited models, use different serialization formats (JSON, XML, YAML, JSONL), and understand how relational fields are represented. It also covers DeserializedObject behavior and common pitfalls.

Continue