简体   繁体   中英

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.

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM