Return single peewee record as dict

You can use ".get()":

one_user = User.select().where(User.name == some_value).dicts().get()

Though you can also add a helper method:

class User(Model):
    @classmethod
    def get_as_dict(cls, expr):
        query = cls.select().where(expr).dicts()
        return query.get()

It's python. You can extend it.


peewee has an extension function model_to_dict, defined in playhouse.shortcuts. From the example:

>>> from playhouse.shortcuts import model_to_dict

>>> user = User.create(username='charlie')
>>> model_to_dict(user)
{'id': 1, 'username': 'charlie'}

Tags:

Python

Peewee