equal
deleted
inserted
replaced
|
1 from django.conf import settings |
|
2 from django.db import models |
|
3 from django.db.models.fields import FieldDoesNotExist |
|
4 |
|
5 class CurrentSiteManager(models.Manager): |
|
6 "Use this to limit objects to those associated with the current site." |
|
7 def __init__(self, field_name='site'): |
|
8 super(CurrentSiteManager, self).__init__() |
|
9 self.__field_name = field_name |
|
10 self.__is_validated = False |
|
11 |
|
12 def get_query_set(self): |
|
13 if not self.__is_validated: |
|
14 try: |
|
15 self.model._meta.get_field(self.__field_name) |
|
16 except FieldDoesNotExist: |
|
17 raise ValueError, "%s couldn't find a field named %s in %s." % \ |
|
18 (self.__class__.__name__, self.__field_name, self.model._meta.object_name) |
|
19 self.__is_validated = True |
|
20 return super(CurrentSiteManager, self).get_query_set().filter(**{self.__field_name + '__id__exact': settings.SITE_ID}) |