Fixed #12040 -- Cleaned up the documentation describing how CurrentSiteManager works. Thanks to elpaso66 for the report, and Gabriel Hurley for the draft patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13168 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-05-09 06:43:18 +00:00
parent 7b98fe98f4
commit 1b16a03dba
1 changed files with 33 additions and 27 deletions

View File

@ -257,12 +257,12 @@ The ``CurrentSiteManager``
.. class:: django.contrib.sites.managers.CurrentSiteManager .. class:: django.contrib.sites.managers.CurrentSiteManager
If :class:`~django.contrib.sites.models.Site`\s play a key role in your application, If :class:`~django.contrib.sites.models.Site` plays a key role in your
consider using the helpful application, consider using the helpful
:class:`~django.contrib.sites.managers.CurrentSiteManager` in your model(s). :class:`~django.contrib.sites.managers.CurrentSiteManager` in your
It's a model :ref:`manager <topics-db-managers>` that automatically filters model(s). It's a model :ref:`manager <topics-db-managers>` that
its queries to include only objects associated with the current automatically filters its queries to include only objects associated
:class:`~django.contrib.sites.models.Site`. with the current :class:`~django.contrib.sites.models.Site`.
Use :class:`~django.contrib.sites.managers.CurrentSiteManager` by adding it to Use :class:`~django.contrib.sites.managers.CurrentSiteManager` by adding it to
your model explicitly. For example:: your model explicitly. For example::
@ -288,16 +288,21 @@ Put another way, these two statements are equivalent::
Photo.objects.filter(site=settings.SITE_ID) Photo.objects.filter(site=settings.SITE_ID)
Photo.on_site.all() Photo.on_site.all()
How did :class:`~django.contrib.sites.managers.CurrentSiteManager` know which How did :class:`~django.contrib.sites.managers.CurrentSiteManager`
field of ``Photo`` was the :class:`~django.contrib.sites.models.Site`? It know which field of ``Photo`` was the
defaults to looking for a field called :class:`~django.contrib.sites.models.Site`? By default,
:class:`~django.contrib.sites.models.Site`. If your model has a :class:`~django.contrib.sites.managers.CurrentSiteManager` looks for a
:class:`~django.db.models.fields.related.ForeignKey` or either a :class:`~django.db.models.fields.related.ForeignKey` called
:class:`~django.db.models.fields.related.ManyToManyField` called something ``site`` or a
*other* than :class:`~django.contrib.sites.models.Site`, you need to explicitly :class:`~django.db.models.fields.related.ManyToManyField` called
pass that as the parameter to ``sites`` to filter on. If you use a field named something other than
:class:`~django.contrib.sites.managers.CurrentSiteManager`. The following model, ``site`` or ``sites`` to identify which
which has a field called ``publish_on``, demonstrates this:: :class:`~django.contrib.sites.models.Site` objects your object is
related to, then you need to explicitly pass the custom field name as
a parameter to
:class:`~django.contrib.sites.managers.CurrentSiteManager` on your
model. The following model, which has a field called ``publish_on``,
demonstrates this::
from django.db import models from django.db import models
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
@ -314,16 +319,17 @@ which has a field called ``publish_on``, demonstrates this::
If you attempt to use :class:`~django.contrib.sites.managers.CurrentSiteManager` If you attempt to use :class:`~django.contrib.sites.managers.CurrentSiteManager`
and pass a field name that doesn't exist, Django will raise a :exc:`ValueError`. and pass a field name that doesn't exist, Django will raise a :exc:`ValueError`.
Finally, note that you'll probably want to keep a normal (non-site-specific) Finally, note that you'll probably want to keep a normal
``Manager`` on your model, even if you use (non-site-specific) ``Manager`` on your model, even if you use
:class:`~django.contrib.sites.managers.CurrentSiteManager`. As explained :class:`~django.contrib.sites.managers.CurrentSiteManager`. As
in the :ref:`manager documentation <topics-db-managers>`, if you define a manager explained in the :ref:`manager documentation <topics-db-managers>`, if
manually, then Django won't create the automatic ``objects = models.Manager()`` you define a manager manually, then Django won't create the automatic
manager for you.Also, note that certain parts of Django -- namely, the Django admin site and ``objects = models.Manager()`` manager for you. Also note that certain
generic views -- use whichever manager is defined *first* in the model, so if parts of Django -- namely, the Django admin site and generic views --
you want your admin site to have access to all objects (not just site-specific use whichever manager is defined *first* in the model, so if you want
ones), put ``objects = models.Manager()`` in your model, before you define your admin site to have access to all objects (not just site-specific
:class:`~django.contrib.sites.managers.CurrentSiteManager`. ones), put ``objects = models.Manager()`` in your model, before you
define :class:`~django.contrib.sites.managers.CurrentSiteManager`.
How Django uses the sites framework How Django uses the sites framework
=================================== ===================================
@ -345,7 +351,7 @@ Here's how Django uses the sites framework:
:class:`~django.contrib.sites.models.Site` is set to the current :class:`~django.contrib.sites.models.Site` is set to the current
:setting:`SITE_ID`, and when comments are listed via the appropriate :setting:`SITE_ID`, and when comments are listed via the appropriate
template tag, only the comments for the current site are displayed. template tag, only the comments for the current site are displayed.
* In the :mod:`flatpages framework <django.contrib.flatpages>`, each * In the :mod:`flatpages framework <django.contrib.flatpages>`, each
flatpage is associated with a particular site. When a flatpage is created, flatpage is associated with a particular site. When a flatpage is created,
you specify its :class:`~django.contrib.sites.models.Site`, and the you specify its :class:`~django.contrib.sites.models.Site`, and the