Code Monkey home page Code Monkey logo

Comments (6)

tiangolo avatar tiangolo commented on April 28, 2024 8

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 😂

from fastapi.

Kurolox avatar Kurolox commented on April 28, 2024 1

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

from fastapi.

Kurolox avatar Kurolox commented on April 28, 2024

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.

from fastapi.

nilsdebruin avatar nilsdebruin commented on April 28, 2024

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

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

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

from fastapi.

github-actions avatar github-actions commented on April 28, 2024

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.

from fastapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.