question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Results as dict/array of dict?

See original GitHub issue

It would be grand if one could do something like this:

    >>> m = Model()
    >>> print m.select()
    [{'id':1, 'name':'foo'},{'id':2, 'name':'bar'}]

i.e., returning each row as a dict. This would be especially handy for returning JSON result sets, and could probably be done using iter to return self._data for the model.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

17reactions
coleifercommented, Nov 8, 2022

Edit

This issue was 8 years old when it was brought to my attention again. Here are the methods you would want to use:

query = SomeModel.select().dicts()
for row in query:  # Each row is a dict.
    print(row['id'])

The information further down is no longer correct.

See comment:

https://github.com/coleifer/peewee/issues/134#issuecomment-704935841


I’m not sure why this should be a part of peewee itself, unless for performance reasons you’re concerned about the overhead of class creation. You can see in flask-peewee some helpers already exist:

You can save by not creating model instances by iterating over the cursor:


db = SqliteDatabase(...)

query = SomeModel.select()
cursor = db.execute(query)

ncols = len(cursor.description)
colnames = [cursor.description[i][0] for i in range(ncols)]
results = []

for row in cursor.fetchall():
    res = {}
    for i in range(ncols):
        res[colnames[i]] = row[i]
    results.append(res)

If you want to get even more low-level, you can take the sql generated by peewee and use a row factory:

3reactions
matthewmavcommented, Oct 11, 2020

Though as @coleifer pointed out that this is a 8 years old post, but as this github issue is among the top finds of search engines regarding this topic and I didn’t find a good example code for usage of the pretty useful dicts() method (thank to @coleifer of course), I decided to post one for future readers:

query = SomeModel.select().dicts()

for row in query:
    print(type(row))

Out:

<class 'dict'>
<class 'dict'>
.
.
<class 'dict'>
Read more comments on GitHub >

github_iconTop Results From Across the Web

List of dicts to/from dict of lists - python - Stack Overflow
Users looking to manipulate tabular data, such as stored in csv files, may find other pydata projects more suitable, such as xarray, pandas,...
Read more >
Python | Convert two lists into a dictionary - GeeksforGeeks
Let's discuss a simple yet useful utility of conversion of two lists into a key:value pair dictionary. Method #1: Naive Method The basic...
Read more >
Dictionaries in Python - Real Python
Dictionaries are Python's implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection...
Read more >
12. Dictionaries | Python Tutorial
Associative arrays consist - like dictionaries of (key, value) pairs, ... The values of a dictionary can be any type of Python data....
Read more >
Collections and Data Structures - Julia Documentation
Dict s look for key=>value pairs, and the key is compared using isequal . ... The output array contains nothing wherever a is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found