marshmallow-sqlalchemy


Namemarshmallow-sqlalchemy JSON
Version 1.4.1 PyPI version JSON
download
home_pageNone
SummarySQLAlchemy integration with the marshmallow (de)serialization library
upload_time2025-02-10 22:36:25
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
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| |marshmallow-support|

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.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)

.. start elevator-pitch

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)

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

.. code-block:: shell-session

   $ pip install -U marshmallow-sqlalchemy


Requires Python >= 3.9, marshmallow >= 3.18.0, and SQLAlchemy >= 1.4.40.

.. end elevator-pitch

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
.. |marshmallow-support| image:: https://badgen.net/badge/marshmallow/3,4?list=1
    :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
    :alt: marshmallow 3|4 compatible


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "marshmallow-sqlalchemy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "Steven Loria <sloria1@gmail.com>",
    "keywords": null,
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/46/80/34c7e1dc67e7ab37c7b763eeb49ba7aa1e203da158421193bb657666b54d/marshmallow_sqlalchemy-1.4.1.tar.gz",
    "platform": null,
    "description": "**********************\nmarshmallow-sqlalchemy\n**********************\n\n|pypi-package| |build-status| |docs| |marshmallow-support|\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\n\nDeclare your models\n===================\n\n.. code-block:: python\n\n    import sqlalchemy as sa\n    from sqlalchemy.orm import (\n        DeclarativeBase,\n        backref,\n        relationship,\n        sessionmaker,\n    )\n\n    from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field\n\n    engine = sa.create_engine(\"sqlite:///:memory:\")\n    Session = sessionmaker(engine)\n\n\n    class Base(DeclarativeBase):\n        pass\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 f\"<Author(name={self.name!r})>\"\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\n.. start elevator-pitch\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\n    with Session() as session:\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    with Session() as session:\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.. code-block:: shell-session\n\n   $ pip install -U marshmallow-sqlalchemy\n\n\nRequires Python >= 3.9, marshmallow >= 3.18.0, and SQLAlchemy >= 1.4.40.\n\n.. end elevator-pitch\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.. |marshmallow-support| image:: https://badgen.net/badge/marshmallow/3,4?list=1\n    :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html\n    :alt: marshmallow 3|4 compatible\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "SQLAlchemy integration with the marshmallow (de)serialization library",
    "version": "1.4.1",
    "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": null,
            "digests": {
                "blake2b_256": "665851c93752a72b865a9726618ea5ff13a4c8548520230ffa6f71ea787fd760",
                "md5": "038013713a51e1a96a45aea670990c09",
                "sha256": "9a3dd88a2b24f425fbffb3fea8aeb7f424a932fc97372a9f1338b7a379396191"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "038013713a51e1a96a45aea670990c09",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 16651,
            "upload_time": "2025-02-10T22:36:22",
            "upload_time_iso_8601": "2025-02-10T22:36:22.476736Z",
            "url": "https://files.pythonhosted.org/packages/66/58/51c93752a72b865a9726618ea5ff13a4c8548520230ffa6f71ea787fd760/marshmallow_sqlalchemy-1.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "468034c7e1dc67e7ab37c7b763eeb49ba7aa1e203da158421193bb657666b54d",
                "md5": "34fe4e321c9ee25709dada59ecff836f",
                "sha256": "b4aa964356d00e178bdb8469a28daa9022b375ff4f5c04f8e2b9aafe1e65c529"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "34fe4e321c9ee25709dada59ecff836f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 51343,
            "upload_time": "2025-02-10T22:36:25",
            "upload_time_iso_8601": "2025-02-10T22:36:25.210130Z",
            "url": "https://files.pythonhosted.org/packages/46/80/34c7e1dc67e7ab37c7b763eeb49ba7aa1e203da158421193bb657666b54d/marshmallow_sqlalchemy-1.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-10 22:36:25",
    "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.45905s