Skip to main content

Add your description here

Project description

Convert Sqlalchmey ORM Classes to Pydantic models

An Sqlalchemy ORM Model

class Pokemon(Base):
    __tablename__ = "pokemon"

    id: Mapped[int] = mapped_column(primary_key=True)
    species: Mapped[str] = mapped_column(String(50), nullable=False)
    level: Mapped[int] = mapped_column(Integer, default=1)
    poke_type: Mapped[str] = mapped_column(String(20))

    trainer_id: Mapped[Optional[int]] = mapped_column(ForeignKey("trainer.id"), nullable=True)

    evolution_id: Mapped[Optional[int]] = mapped_column(ForeignKey("pokemon.id"))

    evolution: Mapped[Optional["Pokemon"]] = relationship()
    owner: Mapped[Optional["Trainer"]] = relationship(back_populates="pokemons")
    items: Mapped[List[PokemonHasItem]] = relationship(back_populates="pokemon")

Usage

from sa2pydantic import sa2pydantic

# Create an Pydantic Create Model for Post Method
PokemonCreate = sa2pydantic(Pokemon,
    name_call=lambda sa: f"{sa.__name__.title()}Create",
    exclude_primarykey=True,
    exclude_rel=True,
    )
# <class 'sa2pydantic.core.PokemonCreate'>
# species: default=PydanticUndefined, annotation=<class 'str'>
# level: default=1, annotation=<class 'int'>
# poke_type: default=PydanticUndefined, annotation=<class 'str'>
# Create an Pydantic Output Model for Get Method
PokemonOut = sa2pydantic(Pokemon,
    name_call=lambda sa: f"{sa.__name__.title()}Out")

# <class 'sa2pydantic.core.PokemonOut'>
# id: default=PydanticUndefined, annotation=<class 'int'>
# species: default=PydanticUndefined, annotation=<class 'str'>
# level: default=1, annotation=<class 'int'>
# poke_type: default=PydanticUndefined, annotation=<class 'str'>
# trainer_id: default=PydanticUndefined, annotation=typing.Optional[int]
# evolution_id: default=PydanticUndefined, annotation=typing.Optional[int]
# evolution: default=None, annotation=typing.Optional[sa2pydantic.core.PokemonOut]
# owner: default=None, annotation=typing.Optional[sa2pydantic.core.TrainerOut]
# items: default=[], annotation=list[sa2pydantic.core.PokemonhasitemOut]
# Create an Pydantic Output Model with all fields as optional with default value
PokemonOutOptional: PY_MODEL = sa2pydantic(Pokemon,
                                   name_call=lambda sa:  f"{sa.__name__.title()}OutOptional",
                                   override_optional=True,
                                   override_default_value=None)

# <class 'sa2pydantic.core.PokemonOutOptional'>
# id: default=None, annotation=typing.Optional[int]
# species: default=None, annotation=typing.Optional[str]
# level: default=None, annotation=typing.Optional[int]
# poke_type: default=None, annotation=typing.Optional[str]
# trainer_id: default=None, annotation=typing.Optional[int]
# evolution_id: default=None, annotation=typing.Optional[int]
# evolution: default=None, annotation=typing.Optional[sa2pydantic.core.PokemonOutOptional]
# owner: default=None, annotation=typing.Optional[sa2pydantic.core.TrainerOutOptional]
# items: default=[], annotation=typing.Optional[list[sa2pydantic.core.PokemonhasitemOutOptional]]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sa2pydantic-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sa2pydantic-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file sa2pydantic-0.1.0.tar.gz.

File metadata

  • Download URL: sa2pydantic-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sa2pydantic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 394b08000d6a0284b2d794da05c60df7ce8c36a2301fea1d69790c0d513ec3c9
MD5 5bffa40613ac6ae5ae40db2fdae36aef
BLAKE2b-256 c723de3895ffeb35dd6433157acee9c28e25075eb132f6c77d613ac8a8a9da6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sa2pydantic-0.1.0.tar.gz:

Publisher: publish.yml on JawaClass/sa2pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sa2pydantic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sa2pydantic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sa2pydantic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a2bddab7b6633c1819c0d1f4fee8df9fc5be763b3cbde0610857bba8cd752dd
MD5 92cd91e0b8aeb73dee2b0b5250d3863c
BLAKE2b-256 1f5390acdb3cfb2088657b52ea19de2197a14319db0ad0922cc01d04e08378f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sa2pydantic-0.1.0-py3-none-any.whl:

Publisher: publish.yml on JawaClass/sa2pydantic

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page