summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorJesper Olsson <73302603+jesperolsson-se@users.noreply.github.com>2023-03-22 19:20:58 +0100
committerGitHub <noreply@github.com>2023-03-22 19:20:58 +0100
commit216eb63883050f6a3bf5d306e42972e7a6b6dff5 (patch)
tree2c4bcb9cf2197edc2b480018313e03ee6f35febd /docs
parentb00046d2c25771bed2242680b08b524a44aa9798 (diff)
downloaddjango-216eb63883050f6a3bf5d306e42972e7a6b6dff5.tar.gz
Fixed #34409 -- Doc'd limitation of dictfetchall() and namedtuplefetchall() examples.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/db/sql.txt13
1 files changed, 11 insertions, 2 deletions
diff --git a/docs/topics/db/sql.txt b/docs/topics/db/sql.txt
index 94a724af04..47a7ea39e1 100644
--- a/docs/topics/db/sql.txt
+++ b/docs/topics/db/sql.txt
@@ -323,7 +323,10 @@ small performance and memory cost, you can return results as a ``dict`` by
using something like this::
def dictfetchall(cursor):
- "Return all rows from a cursor as a dict"
+ """
+ Return all rows from a cursor as a dict.
+ Assume the column names are unique.
+ """
columns = [col[0] for col in cursor.description]
return [dict(zip(columns, row)) for row in cursor.fetchall()]
@@ -336,11 +339,17 @@ immutable and accessible by field names or indices, which might be useful::
def namedtuplefetchall(cursor):
- "Return all rows from a cursor as a namedtuple"
+ """
+ Return all rows from a cursor as a namedtuple.
+ Assume the column names are unique.
+ """
desc = cursor.description
nt_result = namedtuple("Result", [col[0] for col in desc])
return [nt_result(*row) for row in cursor.fetchall()]
+The ``dictfetchall()`` and ``namedtuplefetchall()`` examples assume unique
+column names, since a cursor cannot distinguish columns from different tables.
+
Here is an example of the difference between the three:
.. code-block:: pycon