Django

Code

Show
Ignore:
Timestamp:
06/14/08 09:19:28 (7 months ago)
Author:
russellm
Message:

Fixed some stale documentation that was advising against the use of OneToOneFields?. Post queryset refactor, that warning is no longer required.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/db-api.txt

    r7625 r7633  
    20382038automatically saved to the database. 
    20392039 
     2040One-to-one relationships 
     2041------------------------ 
     2042 
     2043One-to-one relationships are very similar to Many-to-one relationships. 
     2044If you define a OneToOneField on your model, instances of that model will have  
     2045access to the related object via a simple attribute of the model. 
     2046 
     2047For example:: 
     2048 
     2049    class EntryDetail(models.Model): 
     2050        entry = models.OneToOneField(Entry) 
     2051        details = models.TextField() 
     2052 
     2053    ed = EntryDetail.objects.get(id=2) 
     2054    ed.entry # Returns the related Entry object. 
     2055 
     2056The difference comes in reverse queries. The related model in a One-to-one 
     2057relationship also has access to a ``Manager`` object; however, that ``Manager`` 
     2058represents a single object, rather than a collection of objects:: 
     2059 
     2060    e = Entry.objects.get(id=2) 
     2061    e.entrydetail # returns the related EntryDetail object 
     2062 
     2063If no object has been assigned to this relationship, Django will raise 
     2064a ``DoesNotExist`` exception. 
     2065 
     2066Instances can be assigned to the reverse relationship in the same way as  
     2067you would assign the forward relationship:: 
     2068     
     2069    e.entrydetail = ed 
     2070 
    20402071Many-to-many relationships 
    20412072-------------------------- 
     
    20642095``related_name='entries'``, then each ``Author`` instance would have an 
    20652096``entries`` attribute instead of ``entry_set``. 
    2066  
    2067 One-to-one relationships 
    2068 ------------------------ 
    2069  
    2070 The semantics of one-to-one relationships will be changing soon, so we don't 
    2071 recommend you use them. 
    20722097 
    20732098How are the backward relationships possible?