marshmallow-sqlalchemy


Namemarshmallow-sqlalchemy JSON
Version 1.1.0 PyPI version JSON
download
home_pageNone
SummarySQLAlchemy integration with the marshmallow (de)serialization library
upload_time2024-08-14 17:16:41
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
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| |marshmallow3|

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


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "marshmallow-sqlalchemy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Steven Loria <sloria1@gmail.com>",
    "keywords": null,
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/82/f8/2e565045b17dd60b38aafc3242ac44f5403315df08963501d07463d089ac/marshmallow_sqlalchemy-1.1.0.tar.gz",
    "platform": null,
    "description": "**********************\nmarshmallow-sqlalchemy\n**********************\n\n|pypi-package| |build-status| |docs| |marshmallow3|\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.18.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\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "SQLAlchemy integration with the marshmallow (de)serialization library",
    "version": "1.1.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": "0c47edc875e2f51923db12bf8c677595ca3c43bf7d9de8eaccd5d9bebc260c24",
                "md5": "33302a0bb235b8c37a394bd5ee9a44c8",
                "sha256": "cce261148e4c6ec4ee275f3d29352933380a1afa2fd3933f5e9ecd02fdc16ade"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "33302a0bb235b8c37a394bd5ee9a44c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 14419,
            "upload_time": "2024-08-14T17:16:39",
            "upload_time_iso_8601": "2024-08-14T17:16:39.536842Z",
            "url": "https://files.pythonhosted.org/packages/0c/47/edc875e2f51923db12bf8c677595ca3c43bf7d9de8eaccd5d9bebc260c24/marshmallow_sqlalchemy-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "82f82e565045b17dd60b38aafc3242ac44f5403315df08963501d07463d089ac",
                "md5": "1cdbefa0aa35a1205d7e3bb3674743bb",
                "sha256": "2ab092da269dafa8a05d51a58409af71a8d2183958ba47143127dd239e0359d8"
            },
            "downloads": -1,
            "filename": "marshmallow_sqlalchemy-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "1cdbefa0aa35a1205d7e3bb3674743bb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 49475,
            "upload_time": "2024-08-14T17:16:41",
            "upload_time_iso_8601": "2024-08-14T17:16:41.225363Z",
            "url": "https://files.pythonhosted.org/packages/82/f8/2e565045b17dd60b38aafc3242ac44f5403315df08963501d07463d089ac/marshmallow_sqlalchemy-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-14 17:16:41",
    "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: 4.81149s