Ticket #8730: t8730-r8761.diff
File t8730-r8761.diff, 6.2 KB (added by , 16 years ago) |
---|
-
docs/ref/models/fields.txt
diff -r e4be27c92813 docs/ref/models/fields.txt
a b 882 882 The name of the table to create for storing the many-to-many data. If this 883 883 is not provided, Django will assume a default name based upon the names of 884 884 the two tables being joined. 885 886 .. _ref-onetoone: 885 887 886 888 ``OneToOneField`` 887 889 ----------------- … … 897 899 implemented by adding an implicit one-to-one relation from the child 898 900 model to the parent model, for example. 899 901 900 One positional argument is required: the class to which the model will 901 be related. 902 One positional argument is required: the class to which the model will be 903 related. This works exactly the same as it does for :class:`ForeignKey`, 904 including all the options regarding :ref:`recursive <recursive-relationships>` 905 and :ref:`lazy <lazy-relationships>` relationships. 906 907 .. _onetoone-arguments: 902 908 903 909 Additionally, ``OneToOneField`` accepts all of the extra arguments 904 910 accepted by :class:`ForeignKey`, plus one extra argument: 905 911 906 .. attribute: OneToOneField.parent_link912 .. attribute:: OneToOneField.parent_link 907 913 908 914 When ``True`` and used in a model which inherits from another 909 915 (concrete) model, indicates that this field should be used as the -
docs/topics/db/models.txt
diff -r e4be27c92813 docs/topics/db/models.txt
a b 499 499 500 500 501 501 One-to-one relationships 502 ------------------------ 502 ~~~~~~~~~~~~~~~~~~~~~~~~ 503 503 504 One-to-one relationships are very similar to many-to-one relationships. If you 505 define a :class:`~django.db.models.OneToOneField` on your model, instances of 506 that model will have access to the related object via a simple attribute of the 507 model. 504 To define a one-to-one relationship, use 505 :class:`~django.db.models.OneToOneField`. You use it just like any other 506 ``Field`` type: by including it as a class attribute of your model. 508 507 509 For example:: 508 This is most useful on the primary key of an object when that object "extends" 509 another object in some way. 510 510 511 class EntryDetail(models.Model): 512 entry = models.OneToOneField(Entry) 513 details = models.TextField() 511 :class:`~django.db.models.OneToOneField` requires a positional argument: the 512 class to which the model is related. 514 513 515 ed = EntryDetail.objects.get(id=2) 516 ed.entry # Returns the related Entry object. 514 For example, if you're building a database of "places", you would build pretty 515 standard stuff such as address, phone number, etc. in the database. Then, if you 516 wanted to build a database of restaurants on top of the places, instead of 517 repeating yourself and replicating those fields in the ``Restaurant`` model, you 518 could make ``Restaurant`` have a :class:`~django.db.models.OneToOneField` to 519 ``Place`` (because a restaurant "is-a" place). 517 520 518 The difference comes in "reverse" queries. The related model in a one-to-one 519 relationship also has access to a :class:`~django.db.models.Manager` object, but520 that :class:`~django.db.models.Manager` represents a single object, rather than 521 a collection of objects:: 521 As with :class:`~django.db.models.ForeignKey`, a :ref:`relationship to self 522 <recursive-relationships>` can be defined and :ref:`references to as-yet 523 undefined models <lazy-relationships>` can be made; see :ref:`the model field 524 reference <ref-onetoone>` for details. 522 525 523 e = Entry.objects.get(id=2) 524 e.entrydetail # returns the related EntryDetail object 526 .. seealso:: 525 527 526 If no object has been assigned to this relationship, Django will raise 527 a ``DoesNotExist`` exception. 528 See the `One-to-one relationship model example`_ for a full example. 528 529 529 Instances can be assigned to the reverse relationship in the same way as 530 you would assign the forward relationship:: 530 .. _One-to-one relationship model example: http://www.djangoproject.com/documentation/models/one_to_one/ 531 531 532 e.entrydetail = ed 532 **New in Django development version** 533 534 :class:`~django.db.models.OneToOneField` fields also accept one optional argument 535 described in the :ref:`model field reference <ref-onetoone>`. 536 537 :class:`~django.db.models.OneToOneField` classes used to automatically become 538 the primary key on a model. This is no longer true (although you can manually 539 pass in the :attr:`~django.db.models.Field.primary_key` argument if you like). 540 Thus, it's now possible to have multiple fields of type 541 :class:`~django.db.models.OneToOneField` on a single model. 533 542 534 543 Models across files 535 ~~~~~~~~~~~~~~~~~~~ 544 ------------------- 536 545 537 546 It's perfectly OK to relate a model to one from another app. To do this, just 538 547 import the related model at the top of the model that holds your model. Then, -
docs/topics/db/queries.txt
diff -r e4be27c92813 docs/topics/db/queries.txt
a b 916 916 One-to-one relationships 917 917 ------------------------ 918 918 919 The semantics of one-to-one relationships will be changing soon, so we don't 920 recommend you use them. 919 One-to-one relationships are very similar to many-to-one relationships. If you 920 define a :class:`~django.db.models.OneToOneField` on your model, instances of 921 that model will have access to the related object via a simple attribute of the 922 model. 923 924 For example:: 925 926 class EntryDetail(models.Model): 927 entry = models.OneToOneField(Entry) 928 details = models.TextField() 929 930 ed = EntryDetail.objects.get(id=2) 931 ed.entry # Returns the related Entry object. 932 933 The difference comes in "reverse" queries. The related model in a one-to-one 934 relationship also has access to a :class:`~django.db.models.Manager` object, but 935 that :class:`~django.db.models.Manager` represents a single object, rather than 936 a collection of objects:: 937 938 e = Entry.objects.get(id=2) 939 e.entrydetail # returns the related EntryDetail object 940 941 If no object has been assigned to this relationship, Django will raise 942 a ``DoesNotExist`` exception. 943 944 Instances can be assigned to the reverse relationship in the same way as 945 you would assign the forward relationship:: 946 947 e.entrydetail = ed 921 948 922 949 How are the backward relationships possible? 923 950 --------------------------------------------