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.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, nullable=False)
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 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)
session.add(author)
session.add(book)
session.commit()
dump_data = author_schema.dump(author)
print(dump_data)
# {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]}
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.6, marshmallow >= 3.0.0, and SQLAlchemy >= 1.2.0.
Documentation
Documentation is available at https://marshmallow-sqlalchemy.readthedocs.io/ .
Project Links
Changelog: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html
Contributing Guidelines: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/contributing.html
PyPI: https://pypi-hypernode.com/pypi/marshmallow-sqlalchemy
Issues: https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues
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-0.26.0.tar.gz
.
File metadata
- Download URL: marshmallow-sqlalchemy-0.26.0.tar.gz
- Upload date:
- Size: 49.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc335437f74427e47a2fdd68f1c2836c60643a001a1fea602ebd4114ce3f75b0 |
|
MD5 | a46437f5efb051870381621cf03daf9d |
|
BLAKE2b-256 | 75f7de110d93777c089a64b62715918fe912eac15867ff825edb1c8afdb62d22 |
Provenance
File details
Details for the file marshmallow_sqlalchemy-0.26.0-py2.py3-none-any.whl
.
File metadata
- Download URL: marshmallow_sqlalchemy-0.26.0-py2.py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.2.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.7.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cef3d7f275978e2fb502c8693365a5ebe86921d9d021ad2e7401a2e2e3f97a3 |
|
MD5 | b37fca9b11a6d91282c98e3a64a7741b |
|
BLAKE2b-256 | 4ffead2161b6f872499a4e39230c37ce61679d4995b79fe689d20454495cf6bb |