magic-removal: Cleaned up get_in_bulk() and __get_date_list() DB queries to match documentation and improve error checking.
git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1885 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
dde6963869
commit
0389e6261b
|
@ -189,18 +189,12 @@ class Manager(object):
|
|||
assert len(obj_list) == 1, "get_object() returned more than one %s -- it returned %s! Lookup parameters were %s" % (self.klass._meta.object_name, len(obj_list), kwargs)
|
||||
return obj_list[0]
|
||||
|
||||
def get_in_bulk(self, *args, **kwargs):
|
||||
# Separate any list arguments: the first list will be used as the id list; subsequent
|
||||
# lists will be ignored.
|
||||
id_args = filter(lambda arg: isinstance(arg, list), args)
|
||||
# Separate any non-list arguments: these are assumed to be query arguments
|
||||
sql_args = filter(lambda arg: not isinstance(arg, list), args)
|
||||
|
||||
id_list = id_args and id_args[0] or kwargs.get('id_list', [])
|
||||
def get_in_bulk(self, id_list, *args, **kwargs):
|
||||
assert isinstance(id_list, list), "get_in_bulk() must be provided with a list of IDs."
|
||||
assert id_list != [], "get_in_bulk() cannot be passed an empty ID list."
|
||||
kwargs['where'] = ["%s.%s IN (%s)" % (backend.quote_name(self.klass._meta.db_table), backend.quote_name(self.klass._meta.pk.column), ",".join(['%s'] * len(id_list)))]
|
||||
kwargs['params'] = id_list
|
||||
obj_list = self.get_list(*sql_args, **kwargs)
|
||||
obj_list = self.get_list(*args, **kwargs)
|
||||
return dict([(getattr(o, self.klass._meta.pk.attname), o) for o in obj_list])
|
||||
|
||||
def get_values_iterator(self, *args, **kwargs):
|
||||
|
@ -233,14 +227,8 @@ class Manager(object):
|
|||
kwargs['limit'] = 1
|
||||
return self.get_object(*args, **kwargs)
|
||||
|
||||
def __get_date_list(self, field, *args, **kwargs):
|
||||
# Separate any string arguments: the first will be used as the kind
|
||||
kind_args = filter(lambda arg: isinstance(arg, str), args)
|
||||
# Separate any non-list arguments: these are assumed to be query arguments
|
||||
sql_args = filter(lambda arg: not isinstance(arg, str), args)
|
||||
|
||||
def __get_date_list(self, field, kind, *args, **kwargs):
|
||||
from django.db.backends.util import typecast_timestamp
|
||||
kind = kind_args and kind_args[0] or kwargs.get(['kind'],"")
|
||||
assert kind in ("month", "year", "day"), "'kind' must be one of 'year', 'month' or 'day'."
|
||||
order = 'ASC'
|
||||
if kwargs.has_key('order'):
|
||||
|
@ -251,7 +239,7 @@ class Manager(object):
|
|||
if field.null:
|
||||
kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % \
|
||||
(backend.quote_name(self.klass._meta.db_table), backend.quote_name(field.column)))
|
||||
select, sql, params = self._get_sql_clause(*sql_args, **kwargs)
|
||||
select, sql, params = self._get_sql_clause(*args, **kwargs)
|
||||
sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \
|
||||
(backend.get_date_trunc_sql(kind, '%s.%s' % (backend.quote_name(self.klass._meta.db_table),
|
||||
backend.quote_name(field.column))), sql, order)
|
||||
|
|
|
@ -180,6 +180,17 @@ False
|
|||
>>> 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)]
|
||||
|
||||
# Try some bad arguments to __get_date_list
|
||||
>>> Article.objects.get_pub_date_list('badarg')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: 'kind' must be one of 'year', 'month' or 'day'.
|
||||
>>> Article.objects.get_pub_date_list(order='ASC')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: __get_date_list() takes at least 3 non-keyword arguments (2 given)
|
||||
|
||||
|
||||
# An Article instance doesn't have access to the "objects" attribute.
|
||||
# That is only available as a class method.
|
||||
>>> a7.objects.get_list()
|
||||
|
|
|
@ -69,6 +69,18 @@ Article 4
|
|||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: get_in_bulk() cannot be passed an empty ID list.
|
||||
>>> Article.objects.get_in_bulk('foo')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AssertionError: get_in_bulk() must be provided with a list of IDs.
|
||||
>>> Article.objects.get_in_bulk()
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: get_in_bulk() takes at least 2 arguments (1 given)
|
||||
>>> Article.objects.get_in_bulk(headline__startswith='Blah')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: get_in_bulk() takes at least 2 non-keyword arguments (1 given)
|
||||
|
||||
# get_values() is just like get_list(), except it returns a list of
|
||||
# dictionaries instead of object instances -- and you can specify which fields
|
||||
|
|
Loading…
Reference in New Issue