SQLAlchemy integration with the marshmallow (de)serialization library
Project description
Homepage: https://marshmallow-sqlalchemy.readthedocs.io/
SQLAlchemy integration with the marshmallow (de)serialization library.
Declare your models
import sqlalchemy as sa
from sqlalchemy.orm import (
DeclarativeBase,
backref,
relationship,
sessionmaker,
)
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
engine = sa.create_engine("sqlite:///:memory:")
Session = sessionmaker(engine)
class Base(DeclarativeBase):
pass
class Author(Base):
__tablename__ = "authors"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String, nullable=False)
def __repr__(self):
return f"<Author(name={self.name!r})>"
class Book(Base):
__tablename__ = "books"
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String)
author_id = sa.Column(sa.Integer, sa.ForeignKey("authors.id"))
author = relationship("Author", backref=backref("books"))
Base.metadata.create_all(engine)
Generate marshmallow schemas
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
class AuthorSchema(SQLAlchemySchema):
class Meta:
model = Author
load_instance = True # Optional: deserialize to model instances
id = auto_field()
name = auto_field()
books = auto_field()
class BookSchema(SQLAlchemySchema):
class Meta:
model = Book
load_instance = True
id = auto_field()
title = auto_field()
author_id = auto_field()
You can automatically generate fields for a model’s columns using SQLAlchemyAutoSchema. The following schema classes are equivalent to the above.
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
class AuthorSchema(SQLAlchemyAutoSchema):
class Meta:
model = Author
include_relationships = True
load_instance = True
class BookSchema(SQLAlchemyAutoSchema):
class Meta:
model = Book
include_fk = True
load_instance = True
Make sure to declare Models before instantiating Schemas. Otherwise sqlalchemy.orm.configure_mappers() will run too soon and fail.
(De)serialize your data
author = Author(name="Chuck Paluhniuk")
author_schema = AuthorSchema()
book = Book(title="Fight Club", author=author)
with Session() as session:
session.add(author)
session.add(book)
session.commit()
dump_data = author_schema.dump(author)
print(dump_data)
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]}
with Session() as session:
load_data = author_schema.load(dump_data, session=session)
print(load_data)
# <Author(name='Chuck Paluhniuk')>
Get it now
$ pip install -U marshmallow-sqlalchemy
Requires Python >= 3.9, marshmallow >= 3.18.0, and SQLAlchemy >= 1.4.40.
Documentation
Documentation is available at https://marshmallow-sqlalchemy.readthedocs.io/ .
Project links
License
MIT licensed. See the bundled LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file marshmallow_sqlalchemy-1.4.1.tar.gz
.
File metadata
- Download URL: marshmallow_sqlalchemy-1.4.1.tar.gz
- Upload date:
- Size: 51.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4aa964356d00e178bdb8469a28daa9022b375ff4f5c04f8e2b9aafe1e65c529 |
|
MD5 | 34fe4e321c9ee25709dada59ecff836f |
|
BLAKE2b-256 | 468034c7e1dc67e7ab37c7b763eeb49ba7aa1e203da158421193bb657666b54d |
Provenance
The following attestation bundles were made for marshmallow_sqlalchemy-1.4.1.tar.gz
:
Publisher:
build-release.yml
on marshmallow-code/marshmallow-sqlalchemy
-
Statement:
- Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
marshmallow_sqlalchemy-1.4.1.tar.gz
- Subject digest:
b4aa964356d00e178bdb8469a28daa9022b375ff4f5c04f8e2b9aafe1e65c529
- Sigstore transparency entry: 170114486
- Sigstore integration time:
- Permalink:
marshmallow-code/marshmallow-sqlalchemy@a90da5c56ed0812fab3cbce40921b025d487f781
- Branch / Tag:
refs/tags/1.4.1
- Owner: https://github.com/marshmallow-code
- Access:
public
- Token Issuer:
https://token.actions.githubusercontent.com
- Runner Environment:
github-hosted
- Publication workflow:
build-release.yml@a90da5c56ed0812fab3cbce40921b025d487f781
- Trigger Event:
push
- Statement type:
File details
Details for the file marshmallow_sqlalchemy-1.4.1-py3-none-any.whl
.
File metadata
- Download URL: marshmallow_sqlalchemy-1.4.1-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a3dd88a2b24f425fbffb3fea8aeb7f424a932fc97372a9f1338b7a379396191 |
|
MD5 | 038013713a51e1a96a45aea670990c09 |
|
BLAKE2b-256 | 665851c93752a72b865a9726618ea5ff13a4c8548520230ffa6f71ea787fd760 |
Provenance
The following attestation bundles were made for marshmallow_sqlalchemy-1.4.1-py3-none-any.whl
:
Publisher:
build-release.yml
on marshmallow-code/marshmallow-sqlalchemy
-
Statement:
- Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
marshmallow_sqlalchemy-1.4.1-py3-none-any.whl
- Subject digest:
9a3dd88a2b24f425fbffb3fea8aeb7f424a932fc97372a9f1338b7a379396191
- Sigstore transparency entry: 170114487
- Sigstore integration time:
- Permalink:
marshmallow-code/marshmallow-sqlalchemy@a90da5c56ed0812fab3cbce40921b025d487f781
- Branch / Tag:
refs/tags/1.4.1
- Owner: https://github.com/marshmallow-code
- Access:
public
- Token Issuer:
https://token.actions.githubusercontent.com
- Runner Environment:
github-hosted
- Publication workflow:
build-release.yml@a90da5c56ed0812fab3cbce40921b025d487f781
- Trigger Event:
push
- Statement type: