From 42668dcc7993b1fcd2b2ad66fd1d28174e63e8cc Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 14 Jun 2008 14:19:28 +0000 Subject: [PATCH] Fixed some stale documentation that was advising against the use of OneToOneFields. Post queryset refactor, that warning is no longer required. git-svn-id: http://code.djangoproject.com/svn/django/trunk@7633 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/db-api.txt | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/docs/db-api.txt b/docs/db-api.txt index c94eb00f9e..4e1c2c5791 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -2037,6 +2037,37 @@ Each "reverse" operation described in this section has an immediate effect on the database. Every addition, creation and deletion is immediately and automatically saved to the database. +One-to-one relationships +------------------------ + +One-to-one relationships are very similar to Many-to-one relationships. +If you define a OneToOneField on your model, instances of that model will have +access to the related object via a simple attribute of the model. + +For example:: + + class EntryDetail(models.Model): + entry = models.OneToOneField(Entry) + details = models.TextField() + + ed = EntryDetail.objects.get(id=2) + ed.entry # Returns the related Entry object. + +The difference comes in reverse queries. The related model in a One-to-one +relationship also has access to a ``Manager`` object; however, that ``Manager`` +represents a single object, rather than a collection of objects:: + + e = Entry.objects.get(id=2) + e.entrydetail # returns the related EntryDetail object + +If no object has been assigned to this relationship, Django will raise +a ``DoesNotExist`` exception. + +Instances can be assigned to the reverse relationship in the same way as +you would assign the forward relationship:: + + e.entrydetail = ed + Many-to-many relationships -------------------------- @@ -2064,12 +2095,6 @@ above example, if the ``ManyToManyField`` in ``Entry`` had specified ``related_name='entries'``, then each ``Author`` instance would have an ``entries`` attribute instead of ``entry_set``. -One-to-one relationships ------------------------- - -The semantics of one-to-one relationships will be changing soon, so we don't -recommend you use them. - How are the backward relationships possible? --------------------------------------------