Ticket #8730: t8730-r8761.diff

File t8730-r8761.diff, 6.2 KB (added by ramiro, 6 years ago)
  • docs/ref/models/fields.txt

    diff -r e4be27c92813 docs/ref/models/fields.txt
    a b  
    882882    The name of the table to create for storing the many-to-many data. If this
    883883    is not provided, Django will assume a default name based upon the names of
    884884    the two tables being joined.
     885
     886.. _ref-onetoone:
    885887
    886888``OneToOneField``
    887889-----------------
     
    897899implemented by adding an implicit one-to-one relation from the child
    898900model to the parent model, for example.
    899901
    900 One positional argument is required: the class to which the model will
    901 be related.
     902One positional argument is required: the class to which the model will be
     903related. This works exactly the same as it does for :class:`ForeignKey`,
     904including all the options regarding :ref:`recursive <recursive-relationships>`
     905and :ref:`lazy <lazy-relationships>` relationships.
     906
     907.. _onetoone-arguments:
    902908
    903909Additionally, ``OneToOneField`` accepts all of the extra arguments
    904910accepted by :class:`ForeignKey`, plus one extra argument:
    905911
    906 .. attribute: OneToOneField.parent_link
     912.. attribute:: OneToOneField.parent_link
    907913
    908914    When ``True`` and used in a model which inherits from another
    909915    (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  
    499499   
    500500
    501501One-to-one relationships
    502 ------------------------
     502~~~~~~~~~~~~~~~~~~~~~~~~
    503503
    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.
     504To 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.
    508507
    509 For example::
     508This is most useful on the primary key of an object when that object "extends"
     509another object in some way.
    510510
    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
     512class to which the model is related.
    514513
    515     ed = EntryDetail.objects.get(id=2)
    516     ed.entry # Returns the related Entry object.
     514For example, if you're building a database of "places", you would build pretty
     515standard stuff such as address, phone number, etc. in the database. Then, if you
     516wanted to build a database of restaurants on top of the places, instead of
     517repeating yourself and replicating those fields in the ``Restaurant`` model, you
     518could make ``Restaurant`` have a :class:`~django.db.models.OneToOneField` to
     519``Place`` (because a restaurant "is-a" place).
    517520
    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, but
    520 that :class:`~django.db.models.Manager` represents a single object, rather than
    521 a collection of objects::
     521As with :class:`~django.db.models.ForeignKey`, a :ref:`relationship to self
     522<recursive-relationships>` can be defined and :ref:`references to as-yet
     523undefined models <lazy-relationships>` can be made; see :ref:`the model field
     524reference <ref-onetoone>` for details.
    522525
    523     e = Entry.objects.get(id=2)
    524     e.entrydetail # returns the related EntryDetail object
     526.. seealso::
    525527
    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.
    528529
    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/
    531531
    532     e.entrydetail = ed
     532**New in Django development version**
     533
     534:class:`~django.db.models.OneToOneField` fields also accept one optional argument
     535described in the :ref:`model field reference <ref-onetoone>`.
     536
     537:class:`~django.db.models.OneToOneField` classes used to automatically become
     538the primary key on a model. This is no longer true (although you can manually
     539pass in the :attr:`~django.db.models.Field.primary_key` argument if you like).
     540Thus, it's now possible to have multiple fields of type
     541:class:`~django.db.models.OneToOneField` on a single model.
    533542
    534543Models across files
    535 ~~~~~~~~~~~~~~~~~~~
     544-------------------
    536545
    537546It's perfectly OK to relate a model to one from another app. To do this, just
    538547import 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  
    916916One-to-one relationships
    917917------------------------
    918918
    919 The semantics of one-to-one relationships will be changing soon, so we don't
    920 recommend you use them.
     919One-to-one relationships are very similar to many-to-one relationships. If you
     920define a :class:`~django.db.models.OneToOneField` on your model, instances of
     921that model will have access to the related object via a simple attribute of the
     922model.
     923
     924For 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
     933The difference comes in "reverse" queries. The related model in a one-to-one
     934relationship also has access to a :class:`~django.db.models.Manager` object, but
     935that :class:`~django.db.models.Manager` represents a single object, rather than
     936a collection of objects::
     937
     938    e = Entry.objects.get(id=2)
     939    e.entrydetail # returns the related EntryDetail object
     940
     941If no object has been assigned to this relationship, Django will raise
     942a ``DoesNotExist`` exception.
     943
     944Instances can be assigned to the reverse relationship in the same way as
     945you would assign the forward relationship::
     946
     947    e.entrydetail = ed
    921948
    922949How are the backward relationships possible?
    923950--------------------------------------------
Back to Top