Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peewee usage #84

Closed
Kurolox opened this issue Mar 18, 2019 · 6 comments
Closed

Peewee usage #84

Kurolox opened this issue Mar 18, 2019 · 6 comments

Comments

@Kurolox
Copy link

Kurolox commented Mar 18, 2019

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.

@Kurolox Kurolox added the question Question or problem label Mar 18, 2019
@Kurolox
Copy link
Author

Kurolox commented Mar 19, 2019

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: encode/starlette#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.

@nilsdebruin
Copy link

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/)

@tiangolo
Copy link
Member

I haven't used Peewee a lot myself, but I'll try to replicate it to see what happens.

@tiangolo
Copy link
Member

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.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 🤷‍♂️ 😁 ...Although, I haven't integrated this yet, because I just released it 10 min ago 😂

@Kurolox
Copy link
Author

Kurolox commented Jun 21, 2019

That's great! Thanks for your efforts!
I'll be closing this issue then.

@Kurolox Kurolox closed this as completed Jun 21, 2019
@tiangolo tiangolo changed the title [QUESTION] Peewee usage Peewee usage Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions
Copy link
Contributor

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.

@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #8241 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants