diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2006-05-22 05:22:45 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2006-05-22 05:22:45 +0000 |
| commit | 610cffe80e1d6e1f603ce50d248de22c41c871fa (patch) | |
| tree | d845638329954c1ccadb1008dd89f38b3e2df404 /docs | |
| parent | 4cb7a2753f774fa5e1b4df747c16d917d6c4c57c (diff) | |
Added documentation for CurrentSiteManager to docs/sites.txt
git-svn-id: http://code.djangoproject.com/svn/django/trunk@2962 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/sites.txt | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/docs/sites.txt b/docs/sites.txt index 5ab585e77f..153b0141c5 100644 --- a/docs/sites.txt +++ b/docs/sites.txt @@ -213,6 +213,56 @@ To do this, you can use the sites framework. A simple example:: >>> 'http://%s%s' % (Site.objects.get_current().domain, obj.get_absolute_url()) 'http://example.com/mymodel/objects/3/' +The ``CurrentSiteManager`` +========================== + +If ``Site``s play a key role in your application, consider using the helpful +``CurrentSiteManager`` in your model(s). It's a model manager_ that +automatically filters its queries to include only objects associated with the +current ``Site``. + +Use ``CurrentSiteManager`` by adding it to your model explicitly. For example:: + + from django.db import models + from django.contrib.sites.models import Site + from django.contrib.sites.managers import CurrentSiteManager + + class Photo(models.Model): + photo = models.FileField(upload_to='/home/photos') + photographer_name = models.CharField(maxlength=100) + pub_date = models.DateField() + site = models.ForeignKey(Site) + objects = models.Manager() + on_site = CurrentSiteManager() + +With this model, ``Photo.objects.all()`` will return all ``Photo`` objects in +the database, but ``Photo.on_site.all()`` will return only the ``Photo`` +objects associated with the current site, according to the ``SITE_ID`` setting. + +How did ``CurrentSiteManager`` know which field of ``Photo`` was the ``Site``? +It defaults to looking for a field called ``site``. If your model has a +``ForeignKey`` or ``ManyToManyField`` called something *other* than ``site``, +you need to explicitly pass that as the parameter to ``CurrentSiteManager``. +The following model, which has a field called ``publish_on``, demonstrates +this:: + + from django.db import models + from django.contrib.sites.models import Site + from django.contrib.sites.managers import CurrentSiteManager + + class Photo(models.Model): + photo = models.FileField(upload_to='/home/photos') + photographer_name = models.CharField(maxlength=100) + pub_date = models.DateField() + publish_on = models.ForeignKey(Site) + objects = models.Manager() + on_site = CurrentSiteManager('publish_on') + +If you attempt to use ``CurrentSiteManager`` and pass a field name that doesn't +exist, Django will raise a ``ValueError``. + +.. _manager: http://www.djangoproject.com/documentation/model_api/#managers + How Django uses the sites framework =================================== |
