Results as dict/array of dict?
See original GitHub issueIt 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:
- Created 11 years ago
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Edit
This issue was 8 years old when it was brought to my attention again. Here are the methods you would want to use:
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:
If you want to get even more low-level, you can take the sql generated by peewee and use a row factory:
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:Out: