Code

Ticket #8730: t8730-r8761.diff

File t8730-r8761.diff, 6.2 KB (added by ramiro, 6 years ago)
Line 
1diff -r e4be27c92813 docs/ref/models/fields.txt
2--- a/docs/ref/models/fields.txt        Sun Aug 31 08:39:06 2008 -0300
3+++ b/docs/ref/models/fields.txt        Sun Aug 31 13:00:02 2008 -0300
4@@ -882,6 +882,8 @@
5     The name of the table to create for storing the many-to-many data. If this
6     is not provided, Django will assume a default name based upon the names of
7     the two tables being joined.
8+
9+.. _ref-onetoone:
10 
11 ``OneToOneField``
12 -----------------
13@@ -897,13 +899,17 @@
14 implemented by adding an implicit one-to-one relation from the child
15 model to the parent model, for example.
16 
17-One positional argument is required: the class to which the model will
18-be related.
19+One positional argument is required: the class to which the model will be
20+related. This works exactly the same as it does for :class:`ForeignKey`,
21+including all the options regarding :ref:`recursive <recursive-relationships>`
22+and :ref:`lazy <lazy-relationships>` relationships.
23+
24+.. _onetoone-arguments:
25 
26 Additionally, ``OneToOneField`` accepts all of the extra arguments
27 accepted by :class:`ForeignKey`, plus one extra argument:
28 
29-.. attribute: OneToOneField.parent_link
30+.. attribute:: OneToOneField.parent_link
31 
32     When ``True`` and used in a model which inherits from another
33     (concrete) model, indicates that this field should be used as the
34diff -r e4be27c92813 docs/topics/db/models.txt
35--- a/docs/topics/db/models.txt Sun Aug 31 08:39:06 2008 -0300
36+++ b/docs/topics/db/models.txt Sun Aug 31 13:00:02 2008 -0300
37@@ -499,40 +499,49 @@
38     
39 
40 One-to-one relationships
41-------------------------
42+~~~~~~~~~~~~~~~~~~~~~~~~
43 
44-One-to-one relationships are very similar to many-to-one relationships. If you
45-define a :class:`~django.db.models.OneToOneField` on your model, instances of
46-that model will have access to the related object via a simple attribute of the
47-model.
48+To define a one-to-one relationship, use
49+:class:`~django.db.models.OneToOneField`. You use it just like any other
50+``Field`` type: by including it as a class attribute of your model.
51 
52-For example::
53+This is most useful on the primary key of an object when that object "extends"
54+another object in some way.
55 
56-    class EntryDetail(models.Model):
57-        entry = models.OneToOneField(Entry)
58-        details = models.TextField()
59+:class:`~django.db.models.OneToOneField` requires a positional argument: the
60+class to which the model is related.
61 
62-    ed = EntryDetail.objects.get(id=2)
63-    ed.entry # Returns the related Entry object.
64+For example, if you're building a database of "places", you would build pretty
65+standard stuff such as address, phone number, etc. in the database. Then, if you
66+wanted to build a database of restaurants on top of the places, instead of
67+repeating yourself and replicating those fields in the ``Restaurant`` model, you
68+could make ``Restaurant`` have a :class:`~django.db.models.OneToOneField` to
69+``Place`` (because a restaurant "is-a" place).
70 
71-The difference comes in "reverse" queries. The related model in a one-to-one
72-relationship also has access to a :class:`~django.db.models.Manager` object, but
73-that :class:`~django.db.models.Manager` represents a single object, rather than
74-a collection of objects::
75+As with :class:`~django.db.models.ForeignKey`, a :ref:`relationship to self
76+<recursive-relationships>` can be defined and :ref:`references to as-yet
77+undefined models <lazy-relationships>` can be made; see :ref:`the model field
78+reference <ref-onetoone>` for details.
79 
80-    e = Entry.objects.get(id=2)
81-    e.entrydetail # returns the related EntryDetail object
82+.. seealso::
83 
84-If no object has been assigned to this relationship, Django will raise
85-a ``DoesNotExist`` exception.
86+    See the `One-to-one relationship model example`_ for a full example.
87 
88-Instances can be assigned to the reverse relationship in the same way as
89-you would assign the forward relationship::
90+.. _One-to-one relationship model example: http://www.djangoproject.com/documentation/models/one_to_one/
91 
92-    e.entrydetail = ed
93+**New in Django development version**
94+
95+:class:`~django.db.models.OneToOneField` fields also accept one optional argument
96+described in the :ref:`model field reference <ref-onetoone>`.
97+
98+:class:`~django.db.models.OneToOneField` classes used to automatically become
99+the primary key on a model. This is no longer true (although you can manually
100+pass in the :attr:`~django.db.models.Field.primary_key` argument if you like).
101+Thus, it's now possible to have multiple fields of type
102+:class:`~django.db.models.OneToOneField` on a single model.
103 
104 Models across files
105-~~~~~~~~~~~~~~~~~~~
106+-------------------
107 
108 It's perfectly OK to relate a model to one from another app. To do this, just
109 import the related model at the top of the model that holds your model. Then,
110diff -r e4be27c92813 docs/topics/db/queries.txt
111--- a/docs/topics/db/queries.txt        Sun Aug 31 08:39:06 2008 -0300
112+++ b/docs/topics/db/queries.txt        Sun Aug 31 13:00:02 2008 -0300
113@@ -916,8 +916,35 @@
114 One-to-one relationships
115 ------------------------
116 
117-The semantics of one-to-one relationships will be changing soon, so we don't
118-recommend you use them.
119+One-to-one relationships are very similar to many-to-one relationships. If you
120+define a :class:`~django.db.models.OneToOneField` on your model, instances of
121+that model will have access to the related object via a simple attribute of the
122+model.
123+
124+For example::
125+
126+    class EntryDetail(models.Model):
127+        entry = models.OneToOneField(Entry)
128+        details = models.TextField()
129+
130+    ed = EntryDetail.objects.get(id=2)
131+    ed.entry # Returns the related Entry object.
132+
133+The difference comes in "reverse" queries. The related model in a one-to-one
134+relationship also has access to a :class:`~django.db.models.Manager` object, but
135+that :class:`~django.db.models.Manager` represents a single object, rather than
136+a collection of objects::
137+
138+    e = Entry.objects.get(id=2)
139+    e.entrydetail # returns the related EntryDetail object
140+
141+If no object has been assigned to this relationship, Django will raise
142+a ``DoesNotExist`` exception.
143+
144+Instances can be assigned to the reverse relationship in the same way as
145+you would assign the forward relationship::
146+
147+    e.entrydetail = ed
148 
149 How are the backward relationships possible?
150 --------------------------------------------