Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed some bugs in django/contrib/sites/managers and added some error…
… checking git-svn-id: http://code.djangoproject.com/svn/django/trunk@2961 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
1 parent
ddd37b2
commit 4cb7a27
Showing
1 changed file
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,11 +1,26 @@ | |||
from django.db import models | |||
from django.conf import settings | from django.conf import settings | ||
from django.db import models | |||
from django.db.models.fields import FieldDoesNotExist | |||
|
|
||
class CurrentSiteManager(models.Manager): | class CurrentSiteManager(models.Manager): | ||
"Use this to limit objects to those associated with the current site." | "Use this to limit objects to those associated with the current site." | ||
def __init__(self, field_name='site') | def __init__(self, field_name='site'): | ||
super(SiteLimitManager, self).__init__() | super(CurrentSiteManager, self).__init__() | ||
self.__lookup = field_name + '__id__exact' | self.__field_name = field_name | ||
|
|||
def contribute_to_class(self, *args, **kwargs): | |||
# This method is overridden purely to check for errors in | |||
# self.field_name. We can't do this in __init__() because of | |||
# how Managers are implemented -- self.model isn't available | |||
# until after contribute_to_class() is called. | |||
super(CurrentSiteManager, self).contribute_to_class(*args, **kwargs) | |||
try: | |||
self.model._meta.get_field(self.__field_name) | |||
except FieldDoesNotExist: | |||
raise ValueError, "%s couldn't find a field named %s in %s." % \ | |||
(self.__class__.__name__, self.__field_name, self.model._meta.object_name) | |||
self.__lookup = self.__field_name + '__id__exact' | |||
del self.__field_name | |||
|
|
||
def get_query_set(self): | def get_query_set(self): | ||
return super(SiteLimitManager, self).get_query_set().filter(self.__lookup=settings.SITE_ID) | return super(SiteLimitManager, self).get_query_set().filter(**{self.__lookup: settings.SITE_ID}) |