Fixed #14841 -- added xrefs to topics/db/models. Thanks adamv.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14837 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Simon Meers 2010-12-05 23:52:23 +00:00
parent 4800b46746
commit ffa4badbd8
1 changed files with 35 additions and 32 deletions

View File

@ -115,8 +115,8 @@ determine a few things:
* The database column type (e.g. ``INTEGER``, ``VARCHAR``). * The database column type (e.g. ``INTEGER``, ``VARCHAR``).
* The widget to use in Django's admin interface, if you care to use it * The :doc:`widget </ref/forms/widgets>` to use in Django's admin interface,
(e.g. ``<input type="text">``, ``<select>``). if you care to use it (e.g. ``<input type="text">``, ``<select>``).
* The minimal validation requirements, used in Django's admin and in * The minimal validation requirements, used in Django's admin and in
automatically-generated forms. automatically-generated forms.
@ -492,9 +492,10 @@ disabled for many-to-many relationships that use an intermediate model.
The only way to create this type of relationship is to create instances of the The only way to create this type of relationship is to create instances of the
intermediate model. intermediate model.
The ``remove`` method is disabled for similar reasons. However, the The :meth:`~django.db.models.fields.related.RelatedManager.remove` method is
``clear()`` method can be used to remove all many-to-many relationships disabled for similar reasons. However, the
for an instance:: :meth:`~django.db.models.fields.related.RelatedManager.clear` method can be
used to remove all many-to-many relationships for an instance::
# Beatles have broken up # Beatles have broken up
>>> beatles.members.clear() >>> beatles.members.clear()
@ -972,8 +973,8 @@ right).
So a child model does not have access to its parent's :ref:`Meta So a child model does not have access to its parent's :ref:`Meta
<meta-options>` class. However, there are a few limited cases where the child <meta-options>` class. However, there are a few limited cases where the child
inherits behavior from the parent: if the child does not specify an inherits behavior from the parent: if the child does not specify an
:attr:`django.db.models.Options.ordering` attribute or a :attr:`~django.db.models.Options.ordering` attribute or a
:attr:`django.db.models.Options.get_latest_by` attribute, it will inherit :attr:`~django.db.models.Options.get_latest_by` attribute, it will inherit
these from its parent. these from its parent.
If the parent has an ordering and you don't want the child to have any natural If the parent has an ordering and you don't want the child to have any natural
@ -989,12 +990,12 @@ Inheritance and reverse relations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Because multi-table inheritance uses an implicit Because multi-table inheritance uses an implicit
:class:`~django.db.models.fields.OneToOneField` to link the child and :class:`~django.db.models.OneToOneField` to link the child and
the parent, it's possible to move from the parent down to the child, the parent, it's possible to move from the parent down to the child,
as in the above example. However, this uses up the name that is the as in the above example. However, this uses up the name that is the
default :attr:`~django.db.models.ForeignKey.related_name` value for default :attr:`~django.db.models.ForeignKey.related_name` value for
:class:`django.db.models.fields.ForeignKey` and :class:`~django.db.models.ForeignKey` and
:class:`django.db.models.fields.ManyToManyField` relations. If you :class:`~django.db.models.ManyToManyField` relations. If you
are putting those types of relations on a subclass of another model, are putting those types of relations on a subclass of another model,
you **must** specify the you **must** specify the
:attr:`~django.db.models.ForeignKey.related_name` attribute on each :attr:`~django.db.models.ForeignKey.related_name` attribute on each
@ -1002,7 +1003,7 @@ such field. If you forget, Django will raise an error when you run
:djadmin:`validate` or :djadmin:`syncdb`. :djadmin:`validate` or :djadmin:`syncdb`.
For example, using the above ``Place`` class again, let's create another For example, using the above ``Place`` class again, let's create another
subclass with a :class:`~django.db.models.fields.ManyToManyField`:: subclass with a :class:`~django.db.models.ManyToManyField`::
class Supplier(Place): class Supplier(Place):
# Must specify related_name on all relations. # Must specify related_name on all relations.
@ -1013,11 +1014,11 @@ Specifying the parent link field
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As mentioned, Django will automatically create a As mentioned, Django will automatically create a
:class:`~django.db.models.fields.OneToOneField` linking your child :class:`~django.db.models.OneToOneField` linking your child
class back any non-abstract parent models. If you want to control the class back any non-abstract parent models. If you want to control the
name of the attribute linking back to the parent, you can create your name of the attribute linking back to the parent, you can create your
own :class:`~django.db.models.fields.OneToOneField` and set own :class:`~django.db.models.OneToOneField` and set
:attr:`parent_link=True <django.db.models.fields.OneToOneField.parent_link>` :attr:`parent_link=True <django.db.models.OneToOneField.parent_link>`
to indicate that your field is the link back to the parent class. to indicate that your field is the link back to the parent class.
.. _proxy-models: .. _proxy-models:
@ -1045,8 +1046,9 @@ Proxy models are declared like normal models. You tell Django that it's a
proxy model by setting the :attr:`~django.db.models.Options.proxy` attribute of proxy model by setting the :attr:`~django.db.models.Options.proxy` attribute of
the ``Meta`` class to ``True``. the ``Meta`` class to ``True``.
For example, suppose you want to add a method to the standard ``User`` model For example, suppose you want to add a method to the standard
that will be used in your templates. You can do it like this:: :class:`~django.contrib.auth.models.User` model that will be used in your
templates. You can do it like this::
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -1058,37 +1060,38 @@ that will be used in your templates. You can do it like this::
... ...
The ``MyUser`` class operates on the same database table as its parent The ``MyUser`` class operates on the same database table as its parent
``User`` class. In particular, any new instances of ``User`` will also be :class:`~django.contrib.auth.models.User` class. In particular, any new
accessible through ``MyUser``, and vice-versa:: instances of :class:`~django.contrib.auth.models.User` will also be accessible
through ``MyUser``, and vice-versa::
>>> u = User.objects.create(username="foobar") >>> u = User.objects.create(username="foobar")
>>> MyUser.objects.get(username="foobar") >>> MyUser.objects.get(username="foobar")
<MyUser: foobar> <MyUser: foobar>
You could also use a proxy model to define a different default ordering on a You could also use a proxy model to define a different default ordering on a
model. The standard ``User`` model has no ordering defined on it model. The standard :class:`~django.contrib.auth.models.User` model has no
(intentionally; sorting is expensive and we don't want to do it all the time ordering defined on it (intentionally; sorting is expensive and we don't want
when we fetch users). You might want to regularly order by the ``username`` to do it all the time when we fetch users). You might want to regularly order
attribute when you use the proxy. This is easy:: by the ``username`` attribute when you use the proxy. This is easy::
class OrderedUser(User): class OrderedUser(User):
class Meta: class Meta:
ordering = ["username"] ordering = ["username"]
proxy = True proxy = True
Now normal ``User`` queries will be unordered and ``OrderedUser`` queries will Now normal :class:`~django.contrib.auth.models.User` queries will be unordered
be ordered by ``username``. and ``OrderedUser`` queries will be ordered by ``username``.
QuerySets still return the model that was requested QuerySets still return the model that was requested
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
There is no way to have Django return, say, a ``MyUser`` object whenever you There is no way to have Django return, say, a ``MyUser`` object whenever you
query for ``User`` objects. A queryset for ``User`` objects will return those query for :class:`~django.contrib.auth.models.User` objects. A queryset for
types of objects. The whole point of proxy objects is that code relying on the ``User`` objects will return those types of objects. The whole point of proxy
original ``User`` will use those and your own code can use the extensions you objects is that code relying on the original ``User`` will use those and your
included (that no other code is relying on anyway). It is not a way to replace own code can use the extensions you included (that no other code is relying on
the ``User`` (or any other) model everywhere with something of your own anyway). It is not a way to replace the ``User`` (or any other) model
creation. everywhere with something of your own creation.
Base class restrictions Base class restrictions
~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~
@ -1227,5 +1230,5 @@ column name, you can have the same column name appearing in both a child and
an ancestor model for multi-table inheritance (they are columns in two an ancestor model for multi-table inheritance (they are columns in two
different database tables). different database tables).
Django will raise a ``FieldError`` exception if you override any model field Django will raise a :exc:`~django.core.exceptions.FieldError` if you override
in any ancestor model. any model field in any ancestor model.