Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of SITE_ID settings in django?

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.

like image 200
brain storm Avatar asked Jun 17 '14 17:06

brain storm


1 Answers

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()
like image 143
dwitvliet Avatar answered Oct 16 '22 18:10

dwitvliet