marshmallow-sqlalchemy


Namemarshmallow-sqlalchemy JSON
Version 1.0.0 PyPI version JSON
download
home_page
SummarySQLAlchemy integration with the marshmallow (de)serialization library
upload_time2024-01-30 18:11:39
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            **********************
marshmallow-sqlalchemy
**********************

|pypi-package| |build-status| |docs| |marshmallow3| |black|

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

`SQLAlchemy <http://www.sqlalchemy.org/>`_ integration with the  `marshmallow <https://marshmallow.readthedocs.io/en/latest/>`_ (de)serialization library.

Declare your models
===================

.. code-block:: python

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

.. code-block:: python

    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.

.. code-block:: python

    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() <https://docs.sqlalchemy.org/en/latest/orm/mapping_api.html>`_ will run too soon and fail.

(De)serialize your data
=======================

.. code-block:: python

    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.8, marshmallow >= 3.0.0, and SQLAlchemy >= 1.4.40.

Documentation
=============

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

Project Links
=============

- Docs: https://marshmallow-sqlalchemy.readthedocs.io/
- Changelog: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html
- Contributing Guidelines: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/contributing.html
- PyPI: https://pypi.python.org/pypi/marshmallow-sqlalchemy
- Issues: https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues

License
=======

MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/dev/LICENSE>`_ file for more details.


.. |pypi-package| image:: https://badgen.net/pypi/v/marshmallow-sqlalchemy
    :target: https://pypi.org/project/marshmallow-sqlalchemy/
    :alt: Latest version
.. |build-status| image:: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml/badge.svg
    :target: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml
    :alt: Build status
.. |docs| image:: https://readthedocs.org/projects/marshmallow-sqlalchemy/badge/
   :target: http://marshmallow-sqlalchemy.readthedocs.io/
   :alt: Documentation
.. |marshmallow3| image:: https://badgen.net/badge/marshmallow/3
    :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
    :alt: marshmallow 3 compatible
.. |black| image:: https://badgen.net/badge/code%20style/black/000
    :target: https://github.com/ambv/black
    :alt: code style: black


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "marshmallow-sqlalchemy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Steven Loria <sloria1@gmail.com>",
    "keywords": "",
    "author": "",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/ca/e5/6ed1255b8b252cbc063082c85db3690ff40118c891ebda3cf633ec065322/marshmallow_sqlalchemy-1.0.0.tar.gz",
    "platform": null,
    "description": "**********************\nmarshmallow-sqlalchemy\n**********************\n\n|pypi-package| |build-status| |docs| |marshmallow3| |black|\n\nHomepage: https://marshmallow-sqlalchemy.readthedocs.io/\n\n`SQLAlchemy <http://www.sqlalchemy.org/>`_ integration with the  `marshmallow <https://marshmallow.readthedocs.io/en/latest/>`_ (de)serialization library.\n\nDeclare your models\n===================\n\n.. code-block:: python\n\n    import sqlalchemy as sa\n    from sqlalchemy.ext.declarative import declarative_base\n    from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref\n\n    engine = sa.create_engine(\"sqlite:///:memory:\")\n    session = scoped_session(sessionmaker(bind=engine))\n    Base = declarative_base()\n\n\n    class Author(Base):\n        __tablename__ = \"authors\"\n        id = sa.Column(sa.Integer, primary_key=True)\n        name = sa.Column(sa.String, nullable=False)\n\n        def __repr__(self):\n            return \"<Author(name={self.name!r})>\".format(self=self)\n\n\n    class Book(Base):\n        __tablename__ = \"books\"\n        id = sa.Column(sa.Integer, primary_key=True)\n        title = sa.Column(sa.String)\n        author_id = sa.Column(sa.Integer, sa.ForeignKey(\"authors.id\"))\n        author = relationship(\"Author\", backref=backref(\"books\"))\n\n\n    Base.metadata.create_all(engine)\n\nGenerate marshmallow schemas\n============================\n\n.. code-block:: python\n\n    from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field\n\n\n    class AuthorSchema(SQLAlchemySchema):\n        class Meta:\n            model = Author\n            load_instance = True  # Optional: deserialize to model instances\n\n        id = auto_field()\n        name = auto_field()\n        books = auto_field()\n\n\n    class BookSchema(SQLAlchemySchema):\n        class Meta:\n            model = Book\n            load_instance = True\n\n        id = auto_field()\n        title = auto_field()\n        author_id = auto_field()\n\nYou can automatically generate fields for a model's columns using `SQLAlchemyAutoSchema`.\nThe following schema classes are equivalent to the above.\n\n.. code-block:: python\n\n    from marshmallow_sqlalchemy import SQLAlchemyAutoSchema\n\n\n    class AuthorSchema(SQLAlchemyAutoSchema):\n        class Meta:\n            model = Author\n            include_relationships = True\n            load_instance = True\n\n\n    class BookSchema(SQLAlchemyAutoSchema):\n        class Meta:\n            model = Book\n            include_fk = True\n            load_instance = True\n\n\nMake sure to declare `Models` before instantiating `Schemas`. Otherwise `sqlalchemy.orm.configure_mappers() <https://docs.sqlalchemy.org/en/latest/orm/mapping_api.html>`_ will run too soon and fail.\n\n(De)serialize your data\n=======================\n\n.. code-block:: python\n\n    author = Author(name=\"Chuck Paluhniuk\")\n    author_schema = AuthorSchema()\n    book = Book(title=\"Fight Club\", author=author)\n    session.add(author)\n    session.add(book)\n    session.commit()\n\n    dump_data = author_schema.dump(author)\n    print(dump_data)\n    # {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]}\n\n    load_data = author_schema.load(dump_data, session=session)\n    print(load_data)\n    # <Author(name='Chuck Paluhniuk')>\n\nGet it now\n==========\n::\n\n   pip install -U marshmallow-sqlalchemy\n\n\nRequires Python >= 3.8, marshmallow >= 3.0.0, and SQLAlchemy >= 1.4.40.\n\nDocumentation\n=============\n\nDocumentation is available at https://marshmallow-sqlalchemy.readthedocs.io/ .\n\nProject Links\n=============\n\n- Docs: https://marshmallow-sqlalchemy.readthedocs.io/\n- Changelog: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html\n- Contributing Guidelines: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/contributing.html\n- PyPI: https://pypi.python.org/pypi/marshmallow-sqlalchemy\n- Issues: https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues\n\nLicense\n=======\n\nMIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/dev/LICENSE>`_ file for more details.\n\n\n.. |pypi-package| image:: https://badgen.net/pypi/v/marshmallow-sqlalchemy\n    :target: https://pypi.org/project/marshmallow-sqlalchemy/\n    :alt: Latest version\n.. |build-status| image:: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml/badge.svg\n    :target: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml\n    :alt: Build status\n.. |docs| image:: https://readthedocs.org/projects/marshmallow-sqlalchemy/badge/\n   :target: http://marshmallow-sqlalchemy.readthedocs.io/\n   :alt: Documentation\n.. |marshmallow3| image:: https://badgen.net/badge/marshmallow/3\n    :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html\n    :alt: marshmallow 3 compatible\n.. |black| image:: https://badgen.net/badge/code%20style/black/000\n    :target: https://github.com/ambv/black\n    :alt: code style: black\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "SQLAlchemy integration with the marshmallow (de)serialization library",
    "version": "1.0.0",
    "project_urls": {
        "Changelog": "https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html",
        "Funding": "https://opencollective.com/marshmallow",
        "Issues": "https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues",
        "Source": "https://github.com/marshmallow-code/marshmallow-sqlalchemy"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "993295b3e03d41480e5e8963034ed569e94cd5febe64bc23240936b108592bbb",
                "md5": "b820fc092a5fcdd8097899e1826db3e2",
                "sha256": "f415d57809e3555b6323356589aba91e36e4470f35953d3a10c755ac5c3307df"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b820fc092a5fcdd8097899e1826db3e2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14427,
            "upload_time": "2024-01-30T18:11:37",
            "upload_time_iso_8601": "2024-01-30T18:11:37.278849Z",
            "url": "https://files.pythonhosted.org/packages/99/32/95b3e03d41480e5e8963034ed569e94cd5febe64bc23240936b108592bbb/marshmallow_sqlalchemy-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cae56ed1255b8b252cbc063082c85db3690ff40118c891ebda3cf633ec065322",
                "md5": "90483c7c44b3d3948ea405474106debc",
                "sha256": "20a0f2fcdd5bddc86444fa01461f17f9b6a12a8ddd4ca8c9b34fe2f2e35d00a2"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "90483c7c44b3d3948ea405474106debc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 49747,
            "upload_time": "2024-01-30T18:11:39",
            "upload_time_iso_8601": "2024-01-30T18:11:39.363158Z",
            "url": "https://files.pythonhosted.org/packages/ca/e5/6ed1255b8b252cbc063082c85db3690ff40118c891ebda3cf633ec065322/marshmallow_sqlalchemy-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-30 18:11:39",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "marshmallow-code",
    "github_project": "marshmallow-sqlalchemy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "marshmallow-sqlalchemy"
}
        
Elapsed time: 0.18094s