9

When I am doing the flatpage tutorial, I was getting error for SITE_ID not being set. I inserted SITE_ID=1 in the settings file and everything worked fine. But I don't know what this actually means.

I read through the django docs. but I am not totally clear what its use. when will I use something like SITE_ID=2.

On the same note, I used the following snippet in my code without actually knowing what it does:

current_site=Site.objects.get_current()

I assume this has something to do with SITE_ID but may be not.

Some example to demonstrate where SITE_ID could take different values than 1 would help.

jjmontes
  • 24,679
  • 4
  • 39
  • 51
brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

6

It is helpful is you use your code on multiple sites, or if you share a database with another site. An example from the documentation:

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(max_length=100)
    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.

Put another way, these two statements are equivalent:

Photo.objects.filter(site=settings.SITE_ID)
Photo.on_site.all()
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
  • how do you set which SITE_ID corresponds to which site. for ex `1` corresponds to `example.com`, `2` corresponds to `tutorial.com` etc – brain storm Jun 17 '14 at 18:04
  • @brainstorm Each site would likely have their own `settings.py` where it would be set. – dwitvliet Jun 17 '14 at 18:05
0

From documentation:

SITE_ID Default: Not defined

The ID, as an integer, of the current site in the django_site database table. This is used so that application data can hook into specific sites and a single database can manage content for multiple sites.

elmonkeylp
  • 2,674
  • 1
  • 16
  • 14