Skip to main content

SQLAlchemy integration with the marshmallow (de)serialization library

Project description

Latest version Travis-CI

Homepage: http://marshmallow-sqlalchemy.rtfd.org/

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
        sqla_session = session

class BookSchema(ModelSchema):
    class Meta:
        model = Book
        sqla_session = session

author_schema = AuthorSchema()

(De)serialize your data

author = Author(name='Chuck Paluhniuk')
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).data
# <Author(name='Chuck Paluhniuk')>

Get it now

pip install -U marshmallow-sqlalchemy

Documentation

Documentation is available at http://marshmallow-sqlalchemy.readthedocs.org/ .

License

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

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

marshmallow-sqlalchemy-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

marshmallow_sqlalchemy-0.1.0-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file marshmallow-sqlalchemy-0.1.0.tar.gz.

File metadata

File hashes

Hashes for marshmallow-sqlalchemy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff23c4c22299d9ae3b404ed5ca9466622b4e1605c53f51fb8f00eed60d2a6838
MD5 3c1c7538b70dc0b0cecfb4711d56c422
BLAKE2b-256 0e514cbc503efa5876034f12ce95b5b1fc609e30a2f896063f3ba9e1ef20a233

See more details on using hashes here.

Provenance

File details

Details for the file marshmallow_sqlalchemy-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for marshmallow_sqlalchemy-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d32ca7eba9f85ecba80c6d2fa019ed4a1a1617b6fea4f6e78fde85c2cf093f3e
MD5 d2db39830781eda102b97c649ebb0be1
BLAKE2b-256 bc7584ee2ac1e7de9fc526a7af828a4f35255cb2d5ee3eb91acb975777cf10ca

See more details on using hashes here.

Provenance

Supported by

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