0Pricing
Django Academy · Pelajaran

OneToOneField untuk Profil

Perluas model dengan catatan pasangannya

OneToOneField untuk Profil adalah pelajaran Django Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Django Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Django Academy mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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. 👤

Pertanyaan yang Sering Diajukan

Apakah pelajaran “OneToOneField untuk Profil” gratis?

Ya — teks lengkap “OneToOneField untuk Profil” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Django Academy, upgrade ke CoddyKit PRO. Kursus Django Academy mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “OneToOneField untuk Profil”?

Perluas model dengan catatan pasangannya Kamu berlatih Django Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Django Academy?

Tidak diperlukan pengalaman sebelumnya. Django Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.

Berapa lama pelajaran “OneToOneField untuk Profil” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Django Academy ini?

Ya. Setiap pelajaran Django Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. ForeignKey dan on_delete
  2. ManyToManyField dan Model Perantara
  3. OneToOneField untuk Profil
  4. related_name dan Pencarian Terbalik
← Kembali ke Django Academy