Skip to main content

Latest version Travis-CI Documentation marshmallow 3 compatible code style: black

Homepage: https://marshmallow-sqlalchemy.readthedocs.io/

SQLAlchemy integration with the marshmallow (de)serialization library.

Declare your models

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref

engine = sa.create_engine("sqlite:///:memory:")
session = scoped_session(sessionmaker(bind=engine))
Base = declarative_base()


class Author(Base):
    __tablename__ = "authors"
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String)

    def __repr__(self):
        return "<Author(name={self.name!r})>".format(self=self)


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 ModelSchema


class AuthorSchema(ModelSchema):
    class Meta:
        model = Author


class BookSchema(ModelSchema):
    class Meta:
        model = Book
        # optionally attach a Session
        # to use for deserialization
        sqla_session = session


author_schema = AuthorSchema()

(De)serialize your data

author = Author(name="Chuck Paluhniuk")
author_schema = AuthorSchema()
book = Book(title="Fight Club", author=author)
session.add(author)
session.add(book)
session.commit()

dump_data = author_schema.dump(author).data
# {'books': [123], 'id': 321, 'name': 'Chuck Paluhniuk'}

author_schema.load(dump_data, session=session).data
# <Author(name='Chuck Paluhniuk')>

Get it now

pip install -U marshmallow-sqlalchemy

Documentation

Documentation is available at https://marshmallow-sqlalchemy.readthedocs.io/ .

License

MIT licensed. See the bundled LICENSE file for more details.

Release files for marshmallow-muffin-sqlalchemy 0.30.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for marshmallow-muffin-sqlalchemy 0.30.0
File Size Uploaded
marshmallow-muffin-sqlalchemy-0.30.0.tar.gz 11.4 kB Details

Release files / marshmallow-muffin-sqlalchemy-0.30.0.tar.gz

Download URL marshmallow-muffin-sqlalchemy-0.30.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
749a8e96e4450df775fc657ec1ae0e251a62dc975118d8006fc8159ed93a6e1a
BLAKE2b-256 checksum
How to use checksums
9243bf6d9d2fbf4a3f0ff8504be54eacd1d82cb538ae34d2350d0cbf89626370
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/0.12.16 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release history Release notifications | RSS feed

This release

0.30.0 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page