2006-05-22 12:59:22 +08:00
|
|
|
from django.conf import settings
|
2006-05-22 13:17:25 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.db.models.fields import FieldDoesNotExist
|
2006-05-22 12:59:22 +08:00
|
|
|
|
|
|
|
class CurrentSiteManager(models.Manager):
|
|
|
|
"Use this to limit objects to those associated with the current site."
|
2006-05-22 13:17:25 +08:00
|
|
|
def __init__(self, field_name='site'):
|
|
|
|
super(CurrentSiteManager, self).__init__()
|
|
|
|
self.__field_name = field_name
|
2006-05-27 00:30:42 +08:00
|
|
|
self.__is_validated = False
|
2006-05-22 12:59:22 +08:00
|
|
|
|
|
|
|
def get_query_set(self):
|
2006-05-27 00:30:42 +08:00
|
|
|
if not self.__is_validated:
|
|
|
|
try:
|
|
|
|
self.model._meta.get_field(self.__field_name)
|
|
|
|
except FieldDoesNotExist:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise ValueError("%s couldn't find a field named %s in %s." % \
|
|
|
|
(self.__class__.__name__, self.__field_name, self.model._meta.object_name))
|
2006-05-27 00:30:42 +08:00
|
|
|
self.__is_validated = True
|
|
|
|
return super(CurrentSiteManager, self).get_query_set().filter(**{self.__field_name + '__id__exact': settings.SITE_ID})
|