Description
How can I use peewee ORM with fastapi?
Additional context
I'd love to use peewee rather than sqlalchemy for my API. However, all documentation seems to be oriented to SQLAlchemy.
It seems like I can use peewee directly up to a certain point, however some stuff (like ForeignKey relations) don't really work by default.
I think giving a more concise example might show up my current issue when using peewee rather than SQLAlchemy, so I've made this working minimal example.
https://gist.github.com/Kurolox/bd62d486c8c48b8300746f3b479fe5a5
I've also been able to find this mention regarding peewee-async on the starlette issues: https://github.com/encode/starlette/issues/136
While technically I can use the model_to_dict function from the peewee playhouse, I was wondering if there was any way to do it directly in fastAPI in a way like flask-restplus marshalling does it.
Hi Kurolox, good question! I am using Peewee myself and are now experimenting with FastAPI. At this moment I am trying out response models using Pydantic, but this only solves the part that the response will have a known "fixed" output structure and does not process the Peewee to dict model (see: https://fastapi.tiangolo.com/tutorial/response-model/)
I haven't used Peewee a lot myself, but I'll try to replicate it to see what happens.
I just added support for Pydantic's ORM mode in FastAPI 0.30.0. :tada:
ORM mode is new, but it solves most of these cases. You can see the docs here: https://fastapi.tiangolo.com/tutorial/sql-databases/
The docs use examples with SQLAlchemy, but they should work equally for any other ORM. Actually, any object that has attributes (item.name instead of item["name").
It also supports dynamic properties, hybrid attributes, relationships, lazy-loading, etc.
You declare the shape that you need to return in your Pydantic models with all the specific attributes and they take care of extracting the data from your ORM object.
Also, I'm now using quite a lot of Peewee recently for a project :man_shrugging: :grin: ...Although, I haven't integrated this yet, because I just released it 10 min ago :joy:
That's great! Thanks for your efforts!
I'll be closing this issue then.
Most helpful comment
I just added support for Pydantic's ORM mode in FastAPI
0.30.0. :tada:ORM mode is new, but it solves most of these cases. You can see the docs here: https://fastapi.tiangolo.com/tutorial/sql-databases/
The docs use examples with SQLAlchemy, but they should work equally for any other ORM. Actually, any object that has attributes (
item.nameinstead ofitem["name").It also supports dynamic properties, hybrid attributes, relationships, lazy-loading, etc.
You declare the shape that you need to return in your Pydantic models with all the specific attributes and they take care of extracting the data from your ORM object.
Also, I'm now using quite a lot of Peewee recently for a project :man_shrugging: :grin: ...Although, I haven't integrated this yet, because I just released it 10 min ago :joy: