summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-08-01 15:32:42 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-08-01 15:32:42 +0000
commit1a8fc57bf6e4b86fe047d04fea2efe50296634d0 (patch)
tree94a22a5f646fddfa22a3ef6fe73e8f9c1505b379 /django
parent3aa33edeef66ae468eb10dd6b3d35e7053156c45 (diff)
Fixed #214 -- Added get_values() and get_values_iterator() module-level functions to DB API. Thanks, rmunn
git-svn-id: http://code.djangoproject.com/svn/django/trunk@359 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/meta.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/django/core/meta.py b/django/core/meta.py
index b1ac6fe544..a6ef27a526 100644
--- a/django/core/meta.py
+++ b/django/core/meta.py
@@ -562,6 +562,12 @@ class ModelBase(type):
new_mod.get_iterator = curry(function_get_iterator, opts, new_class)
new_mod.get_iterator.__doc__ = "Returns an iterator of %s objects matching the given parameters." % name
+ new_mod.get_values = curry(function_get_values, opts, new_class)
+ new_mod.get_values.__doc__ = "Returns a list of dictionaries matching the given parameters."
+
+ new_mod.get_values_iterator = curry(function_get_values_iterator, opts, new_class)
+ new_mod.get_values_iterator.__doc__ = "Returns an iterator of dictionaries matching the given parameters."
+
new_mod.get_count = curry(function_get_count, opts)
new_mod.get_count.__doc__ = "Returns the number of %s objects matching the given parameters." % name
@@ -1083,6 +1089,31 @@ def function_get_count(opts, **kwargs):
cursor.execute("SELECT COUNT(*)" + sql, params)
return cursor.fetchone()[0]
+def function_get_values_iterator(opts, klass, **kwargs):
+ # select_related and select aren't supported in get_values().
+ kwargs['select_related'] = False
+ kwargs['select'] = {}
+
+ # 'fields' is a list of field names to fetch.
+ try:
+ fields = kwargs.pop('fields')
+ except KeyError: # Default to all fields.
+ fields = [f.name for f in opts.fields]
+
+ cursor = db.db.cursor()
+ _, sql, params = function_get_sql_clause(opts, **kwargs)
+ select = ['%s.%s' % (opts.db_table, f) for f in fields]
+ cursor.execute("SELECT " + (kwargs.get('distinct') and "DISTINCT " or "") + ",".join(select) + sql, params)
+ while 1:
+ rows = cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)
+ if not rows:
+ raise StopIteration
+ for row in rows:
+ yield dict(zip(fields, row))
+
+def function_get_values(opts, klass, **kwargs):
+ return list(function_get_values_iterator(opts, klass, **kwargs))
+
def _fill_table_cache(opts, select, tables, where, old_prefix, cache_tables_seen):
"""
Helper function that recursively populates the select, tables and where (in
@@ -1228,7 +1259,7 @@ def function_get_sql_clause(opts, **kwargs):
_fill_table_cache(opts, select, tables, where, opts.db_table, [opts.db_table])
# Add any additional SELECTs passed in via kwargs.
- if kwargs.get('select', False):
+ if kwargs.get('select'):
select.extend(['(%s) AS %s' % (s[1], s[0]) for s in kwargs['select']])
# ORDER BY clause