Ticket #14841: model-topic-xrefs.diff

File model-topic-xrefs.diff, 7.6 KB (added by Adam Vandenberg, 14 years ago)
  • docs/topics/db/models.txt

    diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt
    index 4467f22..da70a0b 100644
    a b determine a few things:  
    115115
    116116    * The database column type (e.g. ``INTEGER``, ``VARCHAR``).
    117117
    118     * The widget to use in Django's admin interface, if you care to use it
    119       (e.g. ``<input type="text">``, ``<select>``).
     118    * The :doc:`widget </ref/forms/widgets>` to use in Django's admin interface,
     119      if you care to use it (e.g. ``<input type="text">``, ``<select>``).
    120120
    121121    * The minimal validation requirements, used in Django's admin and in
    122122      automatically-generated forms.
    disabled for many-to-many relationships that use an intermediate model.  
    492492The only way to create this type of relationship is to create instances of the
    493493intermediate model.
    494494
    495 The ``remove`` method is disabled for similar reasons. However, the
    496 ``clear()`` method can be used to remove all many-to-many relationships
    497 for an instance::
     495The :meth:`~django.db.models.fields.related.RelatedManager.remove` method is
     496disabled for similar reasons. However, the
     497:meth:`~django.db.models.fields.related.RelatedManager.clear` method can be
     498used to remove all many-to-many relationships for an instance::
    498499
    499500    # Beatles have broken up
    500501    >>> beatles.members.clear()
    right).  
    972973So a child model does not have access to its parent's :ref:`Meta
    973974<meta-options>` class. However, there are a few limited cases where the child
    974975inherits behavior from the parent: if the child does not specify an
    975 :attr:`django.db.models.Options.ordering` attribute or a
    976 :attr:`django.db.models.Options.get_latest_by` attribute, it will inherit
     976:attr:`~django.db.models.Options.ordering` attribute or a
     977:attr:`~django.db.models.Options.get_latest_by` attribute, it will inherit
    977978these from its parent.
    978979
    979980If the parent has an ordering and you don't want the child to have any natural
    Inheritance and reverse relations  
    989990~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    990991
    991992Because multi-table inheritance uses an implicit
    992 :class:`~django.db.models.fields.OneToOneField` to link the child and
     993:class:`~django.db.models.OneToOneField` to link the child and
    993994the parent, it's possible to move from the parent down to the child,
    994995as in the above example. However, this uses up the name that is the
    995996default :attr:`~django.db.models.ForeignKey.related_name` value for
    996 :class:`django.db.models.fields.ForeignKey` and
    997 :class:`django.db.models.fields.ManyToManyField` relations.  If you
     997:class:`~django.db.models.ForeignKey` and
     998:class:`~django.db.models.ManyToManyField` relations.  If you
    998999are putting those types of relations on a subclass of another model,
    9991000you **must** specify the
    10001001:attr:`~django.db.models.ForeignKey.related_name` attribute on each
    such field. If you forget, Django will raise an error when you run  
    10021003:djadmin:`validate` or :djadmin:`syncdb`.
    10031004
    10041005For example, using the above ``Place`` class again, let's create another
    1005 subclass with a :class:`~django.db.models.fields.ManyToManyField`::
     1006subclass with a :class:`~django.db.models.ManyToManyField`::
    10061007
    10071008    class Supplier(Place):
    10081009        # Must specify related_name on all relations.
    Specifying the parent link field  
    10131014~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10141015
    10151016As mentioned, Django will automatically create a
    1016 :class:`~django.db.models.fields.OneToOneField` linking your child
     1017:class:`~django.db.models.OneToOneField` linking your child
    10171018class back any non-abstract parent models. If you want to control the
    10181019name of the attribute linking back to the parent, you can create your
    1019 own :class:`~django.db.models.fields.OneToOneField` and set
    1020 :attr:`parent_link=True <django.db.models.fields.OneToOneField.parent_link>`
     1020own :class:`~django.db.models.OneToOneField` and set
     1021:attr:`parent_link=True <django.db.models.OneToOneField.parent_link>`
    10211022to indicate that your field is the link back to the parent class.
    10221023
    10231024.. _proxy-models:
    Proxy models are declared like normal models. You tell Django that it's a  
    10451046proxy model by setting the :attr:`~django.db.models.Options.proxy` attribute of
    10461047the ``Meta`` class to ``True``.
    10471048
    1048 For example, suppose you want to add a method to the standard ``User`` model
    1049 that will be used in your templates. You can do it like this::
     1049For example, suppose you want to add a method to the standard
     1050:class:`~django.contrib.auth.models.User` model that will be used in your
     1051templates. You can do it like this::
    10501052
    10511053    from django.contrib.auth.models import User
    10521054
    that will be used in your templates. You can do it like this::  
    10581060            ...
    10591061
    10601062The ``MyUser`` class operates on the same database table as its parent
    1061 ``User`` class. In particular, any new instances of ``User`` will also be
    1062 accessible through ``MyUser``, and vice-versa::
     1063:class:`~django.contrib.auth.models.User` class. In particular, any new
     1064instances of :class:`~django.contrib.auth.models.User` will also be accessible
     1065through ``MyUser``, and vice-versa::
    10631066
    10641067    >>> u = User.objects.create(username="foobar")
    10651068    >>> MyUser.objects.get(username="foobar")
    10661069    <MyUser: foobar>
    10671070
    10681071You could also use a proxy model to define a different default ordering on a
    1069 model. The standard ``User`` model has no ordering defined on it
    1070 (intentionally; sorting is expensive and we don't want to do it all the time
    1071 when we fetch users). You might want to regularly order by the ``username``
    1072 attribute when you use the proxy. This is easy::
     1072model. The standard :class:`~django.contrib.auth.models.User` model has no
     1073ordering defined on it (intentionally; sorting is expensive and we don't want
     1074to do it all the time when we fetch users). You might want to regularly order
     1075by the ``username`` attribute when you use the proxy. This is easy::
    10731076
    10741077    class OrderedUser(User):
    10751078        class Meta:
    10761079            ordering = ["username"]
    10771080            proxy = True
    10781081
    1079 Now normal ``User`` queries will be unordered and ``OrderedUser`` queries will
    1080 be ordered by ``username``.
     1082Now normal :class:`~django.contrib.auth.models.User` queries will be unordered
     1083and ``OrderedUser`` queries will be ordered by ``username``.
    10811084
    10821085QuerySets still return the model that was requested
    10831086~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    10841087
    10851088There is no way to have Django return, say, a ``MyUser`` object whenever you
    1086 query for ``User`` objects. A queryset for ``User`` objects will return those
    1087 types of objects. The whole point of proxy objects is that code relying on the
    1088 original ``User`` will use those and your own code can use the extensions you
    1089 included (that no other code is relying on anyway). It is not a way to replace
    1090 the ``User`` (or any other) model everywhere with something of your own
    1091 creation.
     1089query for :class:`~django.contrib.auth.models.User` objects. A queryset for
     1090``User`` objects will return those types of objects. The whole point of proxy
     1091objects is that code relying on the original ``User`` will use those and your
     1092own code can use the extensions you included (that no other code is relying on
     1093anyway). It is not a way to replace the ``User`` (or any other) model
     1094everywhere with something of your own creation.
    10921095
    10931096Base class restrictions
    10941097~~~~~~~~~~~~~~~~~~~~~~~
    column name, you can have the same column name appearing in both a child and  
    12271230an ancestor model for multi-table inheritance (they are columns in two
    12281231different database tables).
    12291232
    1230 Django will raise a ``FieldError`` exception if you override any model field
    1231 in any ancestor model.
     1233Django will raise a :exc:`~django.core.exceptions.FieldError` if you override
     1234any model field in any ancestor model.
Back to Top