1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

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:
Russell Keith-Magee 2006-01-09 14:34:17 +00:00
parent dde6963869
commit 0389e6261b
3 changed files with 28 additions and 17 deletions

View File

@ -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) 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] return obj_list[0]
def get_in_bulk(self, *args, **kwargs): def get_in_bulk(self, id_list, *args, **kwargs):
# Separate any list arguments: the first list will be used as the id list; subsequent assert isinstance(id_list, list), "get_in_bulk() must be provided with a list of IDs."
# 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', [])
assert id_list != [], "get_in_bulk() cannot be passed an empty ID list." 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['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 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]) return dict([(getattr(o, self.klass._meta.pk.attname), o) for o in obj_list])
def get_values_iterator(self, *args, **kwargs): def get_values_iterator(self, *args, **kwargs):
@ -233,14 +227,8 @@ class Manager(object):
kwargs['limit'] = 1 kwargs['limit'] = 1
return self.get_object(*args, **kwargs) return self.get_object(*args, **kwargs)
def __get_date_list(self, field, *args, **kwargs): def __get_date_list(self, field, kind, *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)
from django.db.backends.util import typecast_timestamp 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'." assert kind in ("month", "year", "day"), "'kind' must be one of 'year', 'month' or 'day'."
order = 'ASC' order = 'ASC'
if kwargs.has_key('order'): if kwargs.has_key('order'):
@ -251,7 +239,7 @@ class Manager(object):
if field.null: if field.null:
kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % \ kwargs.setdefault('where', []).append('%s.%s IS NOT NULL' % \
(backend.quote_name(self.klass._meta.db_table), backend.quote_name(field.column))) (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' % \ 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.get_date_trunc_sql(kind, '%s.%s' % (backend.quote_name(self.klass._meta.db_table),
backend.quote_name(field.column))), sql, order) backend.quote_name(field.column))), sql, order)

View File

@ -180,6 +180,17 @@ 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)]
# 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. # An Article instance doesn't have access to the "objects" attribute.
# That is only available as a class method. # That is only available as a class method.
>>> a7.objects.get_list() >>> a7.objects.get_list()

View File

@ -69,6 +69,18 @@ Article 4
Traceback (most recent call last): Traceback (most recent call last):
... ...
AssertionError: get_in_bulk() cannot be passed an empty ID list. 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 # get_values() is just like get_list(), except it returns a list of
# dictionaries instead of object instances -- and you can specify which fields # dictionaries instead of object instances -- and you can specify which fields