0Pricing
Django Academy · Lesson

OneToOneField for Profiles

Extend a model with a paired record.

OneToOneField for Profiles is a free Django Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Exactly One Each Way

Sometimes a row pairs with exactly one other row, both ways. That is a one-to-one relationship, perfect for splitting data cleanly.

The Profile Pattern

The classic use is a user profile: each user gets one profile, and each profile belongs to one user. Extra fields live separately.

Declaring OneToOneField

Add a OneToOneField pointing at the other model. It behaves like a ForeignKey with a built-in uniqueness guarantee.

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

Unique Under the Hood

A OneToOneField is really a ForeignKey marked unique. That constraint stops two profiles from ever sharing one user.

on_delete Still Applies

Like any link, it needs on_delete. With CASCADE, deleting the user removes their profile too in one step.

user = models.OneToOneField(User, on_delete=models.CASCADE)

Why Not Just Add Fields?

You could pile fields onto the original model, but a separate profile keeps it tidy and avoids touching tables you do not own.

Forward Access

From the profile, read the linked user straight through the field. It returns the single connected object.

profile = Profile.objects.first()
print(profile.user.username)

Reverse Access

The reverse side is special: from a user you reach the profile with a lowercase model name, no _set suffix needed.

user = User.objects.first()
print(user.profile.bio)

Creating a Profile

Make a profile by passing the user it pairs with. The unique constraint blocks a second profile for that same user.

Profile.objects.create(user=user, bio="Hello!")

Auto-Creating with Signals

A common trick is a post_save signal that builds a profile the moment a user is created, so the pair always exists.

Querying Across the Pair

Filter through the link with the double underscore, reaching the profile's fields from the user side or vice versa.

User.objects.filter(profile__bio__icontains="django")

Quick Check

Each user must have at most one profile, and each profile one user. Which field models this?

Recap

Great job! A OneToOneField pairs two rows uniquely, and the reverse is reached by the model's lowercase name. 👤

Frequently asked questions

Is the “OneToOneField for Profiles” lesson free?

Yes — the full text of “OneToOneField for Profiles” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “OneToOneField for Profiles”?

Extend a model with a paired record. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “OneToOneField for Profiles” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ForeignKey and on_delete
  2. ManyToManyField and Through Models
  3. OneToOneField for Profiles
  4. related_name and Reverse Lookups
← Back to Django Academy