magic-removal: Added Manager.__get__ method that forbids access by instances. Also added unit tests. Thanks, rjwittams
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1615 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0438b6848b
commit
257f7090ef
|
@ -1194,10 +1194,15 @@ class Model:
|
||||||
|
|
||||||
__set_many_to_many_objects.alters_data = True
|
__set_many_to_many_objects.alters_data = True
|
||||||
|
|
||||||
class Manager:
|
class Manager(object):
|
||||||
def __init__(self, model_class):
|
def __init__(self, model_class):
|
||||||
self.klass = model_class
|
self.klass = model_class
|
||||||
|
|
||||||
|
def __get__(self, instance, type=None):
|
||||||
|
if instance != None:
|
||||||
|
raise AttributeError, "Manager isn't accessible via %s instances" % self.klass.__name__
|
||||||
|
return self
|
||||||
|
|
||||||
def _prepare(self):
|
def _prepare(self):
|
||||||
# Creates some methods once self.klass._meta has been populated.
|
# Creates some methods once self.klass._meta has been populated.
|
||||||
if self.klass._meta.get_latest_by:
|
if self.klass._meta.get_latest_by:
|
||||||
|
|
|
@ -180,6 +180,18 @@ False
|
||||||
>>> Article.objects.get_pub_date_list('day', order='DESC')
|
>>> Article.objects.get_pub_date_list('day', order='DESC')
|
||||||
[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)]
|
[datetime.datetime(2005, 7, 31, 0, 0), datetime.datetime(2005, 7, 30, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 7, 28, 0, 0)]
|
||||||
|
|
||||||
|
# An Article instance doesn't have access to the "objects" attribute.
|
||||||
|
# That is only available as a class method.
|
||||||
|
>>> a7.objects.get_list()
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
AttributeError: Manager isn't accessible via Article instances
|
||||||
|
|
||||||
|
>>> a7.objects
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
AttributeError: Manager isn't accessible via Article instances
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
Loading…
Reference in New Issue