FastAPI now has full support for all ORMs: SQLAlchemy, Peewee, Mongoengine, etc.

1 points by tiangolo ↗ HN
FastAPI now supports all* ORMs by deeply integrating with Pydantic's ORM mode.

That allows you to declare the data you want to return to an API client using Pydantic models and FastAPI will extract that data (via Pydantic) from any object (any ORM) that you return in your function.

That means that you can return a model using SQLAlchemy, Peewee, GINO, Mongoengine, ElasticSearch DSL, etc. Anything that has an interface like user.name (compared to user["name"]).

And it will be converted to JSON and returned correctly to the API clients.

Note: You can still return dict-like objects and they will work as usual. ORM mode is in addition to that.

You can, for example, declare a Pydantic model with a sub-model for a relationship and the specific fields that you want to return from that relationship. And FastAPI (using Pydantic ORM mode) will make the ORM fetch the data for that sub-model (for the relationship), will serialize it and return the JSON version in the request.

All this while being automatically documented, validated, converted (serialized), etc.

This includes support for things like:

* lazy-loading

* hybrid properties

* relationships

* etc.

Check the updated tutorial here: https://fastapi.tiangolo.com/tutorial/sql-databases/

* Note: The integration is actually independent of each specific ORM, it doesn't assume anything about them, not even expecting them to expose dict-like interfaces. That way, it's compatible with virtually all of them.

0 comments

[ 4.7 ms ] story [ 11.4 ms ] thread

No comments yet.