OneToOneField für Profile
Ein Model mit einem zugehörigen Datensatz erweitern
OneToOneField für Profile ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 👤
Lerne Python mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 30
- Lektionen
- 120
Häufig gestellte Fragen
Ist die Lektion „OneToOneField für Profile“ kostenlos?
Ja — der vollständige Text von „OneToOneField für Profile“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „OneToOneField für Profile“?
Ein Model mit einem zugehörigen Datensatz erweitern Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „OneToOneField für Profile“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- ForeignKey und on_delete
- ManyToManyField und Through-Modelle
- OneToOneField für Profile
- related_name und umgekehrte Lookups