Hyperlinks for pydantic models
Read a blog post from the library author
discussing why this library exists.
In a typical web application relationships between resources are modeled by
primary and foreign keys in a database (integers, UUIDs, etc.). The most natural
way to represent relationships in REST APIs is by URLs to the related resources
(explained in this blog).
hrefs makes it easy to add hyperlinks between pydantic models in a declarative way. Just
Href field and the library will automatically convert between keys
from hrefs import Href, BaseReferrableModel
from pydantic import BaseModel
details_view = "get_book"
def get_book(id: int) -> Book:
def get_library() -> Library:
Will serialize into:
def post_library(library: Library):
Assuming the request contains
Will deserialize into: [1,2,3]
write_to_database([href.key for href in library.books])
hrefs was written especially with FastAPI
in mind, but integrates into any application or framework using pydantic to
parse and serialize models.
Check out the documentation to get started!
Install the library using
pip or your favorite package management tool: