3 from django
.db
.models
.query
import QuerySet
, EmptyQuerySet
, insert_query
4 from django
.dispatch
import dispatcher
5 from django
.db
.models
import signals
6 from django
.db
.models
.fields
import FieldDoesNotExist
8 def ensure_default_manager(sender
):
10 if not getattr(cls
, '_default_manager', None) and not cls
._meta
.abstract
:
11 # Create the default manager, if needed.
13 cls
._meta
.get_field('objects')
14 raise ValueError, "Model %s must specify a custom Manager, because it has a field named 'objects'" % cls
.__name
__
15 except FieldDoesNotExist
:
17 cls
.add_to_class('objects', Manager())
19 dispatcher
.connect(ensure_default_manager
, signal
=signals
.class_prepared
)
21 class Manager(object):
22 # Tracks each time a Manager instance is created. Used to retain order.
26 super(Manager
, self
).__init
__()
27 # Increase the creation counter, and save our local copy.
28 self
.creation_counter
= Manager
.creation_counter
29 Manager
.creation_counter
+= 1
32 def contribute_to_class(self
, model
, name
):
33 # TODO: Use weakref because of possible memory leak / circular reference.
35 setattr(model
, name
, ManagerDescriptor(self
))
36 if not getattr(model
, '_default_manager', None) or self
.creation_counter
< model
._default
_manager
.creation_counter
:
37 model
._default
_manager
= self
39 def _copy_to_model(self
, model
):
41 Makes a copy of the manager and assigns it to 'model', which should be
42 a child of the existing model (used when inheriting a manager from an
45 assert issubclass(model
, self
.model
)
50 #######################
51 # PROXIES TO QUERYSET #
52 #######################
54 def get_empty_query_set(self
):
55 return EmptyQuerySet(self
.model
)
57 def get_query_set(self
):
58 """Returns a new QuerySet object. Subclasses can override this method
59 to easily customize the behavior of the Manager.
61 return QuerySet(self
.model
)
64 return self
.get_empty_query_set()
67 return self
.get_query_set()
70 return self
.get_query_set().count()
72 def dates(self
, *args
, **kwargs
):
73 return self
.get_query_set().dates(*args
, **kwargs
)
75 def distinct(self
, *args
, **kwargs
):
76 return self
.get_query_set().distinct(*args
, **kwargs
)
78 def extra(self
, *args
, **kwargs
):
79 return self
.get_query_set().extra(*args
, **kwargs
)
81 def get(self
, *args
, **kwargs
):
82 return self
.get_query_set().get(*args
, **kwargs
)
84 def get_or_create(self
, **kwargs
):
85 return self
.get_query_set().get_or_create(**kwargs
)
87 def create(self
, **kwargs
):
88 return self
.get_query_set().create(**kwargs
)
90 def filter(self
, *args
, **kwargs
):
91 return self
.get_query_set().filter(*args
, **kwargs
)
93 def complex_filter(self
, *args
, **kwargs
):
94 return self
.get_query_set().complex_filter(*args
, **kwargs
)
96 def exclude(self
, *args
, **kwargs
):
97 return self
.get_query_set().exclude(*args
, **kwargs
)
99 def in_bulk(self
, *args
, **kwargs
):
100 return self
.get_query_set().in_bulk(*args
, **kwargs
)
102 def iterator(self
, *args
, **kwargs
):
103 return self
.get_query_set().iterator(*args
, **kwargs
)
105 def latest(self
, *args
, **kwargs
):
106 return self
.get_query_set().latest(*args
, **kwargs
)
108 def order_by(self
, *args
, **kwargs
):
109 return self
.get_query_set().order_by(*args
, **kwargs
)
111 def select_related(self
, *args
, **kwargs
):
112 return self
.get_query_set().select_related(*args
, **kwargs
)
114 def values(self
, *args
, **kwargs
):
115 return self
.get_query_set().values(*args
, **kwargs
)
117 def values_list(self
, *args
, **kwargs
):
118 return self
.get_query_set().values_list(*args
, **kwargs
)
120 def update(self
, *args
, **kwargs
):
121 return self
.get_query_set().update(*args
, **kwargs
)
123 def reverse(self
, *args
, **kwargs
):
124 return self
.get_query_set().reverse(*args
, **kwargs
)
126 def _insert(self
, values
, **kwargs
):
127 return insert_query(self
.model
, values
, **kwargs
)
129 def _update(self
, values
, **kwargs
):
130 return self
.get_query_set()._update
(values
, **kwargs
)
132 class ManagerDescriptor(object):
133 # This class ensures managers aren't accessible via model instances.
134 # For example, Poll.objects works, but poll_obj.objects raises AttributeError.
135 def __init__(self
, manager
):
136 self
.manager
= manager
138 def __get__(self
, instance
, type=None):
140 raise AttributeError, "Manager isn't accessible via %s instances" % type.__name
__
143 class EmptyManager(Manager
):
144 def get_query_set(self
):
145 return self
.get_empty_query_set()