SQLAlchemy integration with the marshmallow (de)serialization library
Project Links
Meta
Maintainer: Steven Loria
Requires Python: >=3.9
Classifiers
Intended Audience
- Developers
License
- OSI Approved :: MIT License
Programming Language
- Python :: 3
- Python :: 3.9
- Python :: 3.10
- Python :: 3.11
- Python :: 3.12
- Python :: 3.13
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.
1.4.2
Apr 09, 2025
1.4.1
Feb 10, 2025
1.4.0
Jan 20, 2025
1.3.0
Jan 11, 2025
1.2.0
Jan 09, 2025
1.1.1
Jan 07, 2025
1.1.0
Aug 14, 2024
1.0.0
Jan 30, 2024
0.30.0
Jan 07, 2024
0.29.0
Feb 27, 2023
0.28.2
Feb 23, 2023
0.28.1
Jul 18, 2022
0.28.0
Mar 10, 2022
0.27.0
Dec 18, 2021
0.26.1
Jun 05, 2021
0.26.0
May 27, 2021
0.25.0
May 02, 2021
0.24.3
Apr 26, 2021
0.24.2
Feb 07, 2021
0.24.1
Nov 15, 2020
0.24.0
Oct 21, 2020
0.23.1
May 30, 2020
0.23.0
Apr 26, 2020
0.22.3
Mar 01, 2020
0.22.2
Feb 10, 2020
0.22.1
Feb 10, 2020
0.22.0
Feb 09, 2020
0.21.0
Dec 05, 2019
0.20.0
Dec 01, 2019
0.19.0
Sep 06, 2019
0.18.0
Sep 06, 2019
0.17.2
Sep 01, 2019
0.17.1
Aug 31, 2019
0.17.0
Jun 22, 2019
0.16.4
Jun 15, 2019
0.16.3
May 05, 2019
0.16.2
Apr 11, 2019
0.16.1
Mar 11, 2019
0.16.0
Feb 04, 2019
0.15.0
Nov 05, 2018
0.14.2
Nov 03, 2018
0.14.1
Aug 15, 2018
0.14.0
May 29, 2018
0.13.2
Oct 24, 2017
0.13.1
Apr 07, 2017
0.13.0
Mar 12, 2017
0.12.1
Jan 05, 2017
0.12.0
Oct 08, 2016
0.11.0
Oct 02, 2016
0.10.0
Aug 15, 2016
0.9.0
Jul 02, 2016
0.8.1
Feb 22, 2016
0.8.0
Dec 29, 2015
0.7.1
Dec 13, 2015
0.7.0
Dec 08, 2015
0.6.0
Oct 01, 2015
0.5.0
Sep 27, 2015
0.4.1
Sep 14, 2015
0.4.0
Sep 03, 2015
0.3.0
Aug 28, 2015
0.2.0
May 03, 2015
0.1.1
May 02, 2015
0.1.0
Apr 28, 2015