Peewee usage #8241
-
|
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. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
|
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 While technically I can use the |
Beta Was this translation helpful? Give feedback.
-
|
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/) |
Beta Was this translation helpful? Give feedback.
-
|
I haven't used Peewee a lot myself, but I'll try to replicate it to see what happens. |
Beta Was this translation helpful? Give feedback.
-
|
I just added support for Pydantic's ORM mode in FastAPI 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 ( 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 🤷♂️ 😁 ...Although, I haven't integrated this yet, because I just released it 10 min ago 😂 |
Beta Was this translation helpful? Give feedback.
-
|
That's great! Thanks for your efforts! |
Beta Was this translation helpful? Give feedback.
-
|
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
Beta Was this translation helpful? Give feedback.
I just added support for Pydantic's ORM mode in FastAPI
0.30.0. 🎉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 🤷♂️ 😁 ...Alt…