METADATA 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. Metadata-Version: 2.1
  2. Name: marshmallow-sqlalchemy
  3. Version: 0.28.2
  4. Summary: SQLAlchemy integration with the marshmallow (de)serialization library
  5. Home-page: https://github.com/marshmallow-code/marshmallow-sqlalchemy
  6. Author: Steven Loria
  7. Author-email: sloria1@gmail.com
  8. License: MIT
  9. Project-URL: Changelog, https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html
  10. Project-URL: Issues, https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues
  11. Project-URL: Funding, https://opencollective.com/marshmallow
  12. Keywords: sqlalchemy marshmallow
  13. Classifier: Intended Audience :: Developers
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Natural Language :: English
  16. Classifier: Programming Language :: Python :: 3.7
  17. Classifier: Programming Language :: Python :: 3.8
  18. Classifier: Programming Language :: Python :: 3.9
  19. Classifier: Programming Language :: Python :: 3.10
  20. Requires-Python: >=3.7
  21. License-File: LICENSE
  22. License-File: AUTHORS.rst
  23. Requires-Dist: marshmallow (>=3.0.0)
  24. Requires-Dist: SQLAlchemy (<2.0,>=1.3.0)
  25. Requires-Dist: packaging (>=21.3)
  26. Provides-Extra: dev
  27. Requires-Dist: pytest ; extra == 'dev'
  28. Requires-Dist: pytest-lazy-fixture (>=0.6.2) ; extra == 'dev'
  29. Requires-Dist: flake8 (==6.0.0) ; extra == 'dev'
  30. Requires-Dist: flake8-bugbear (==23.2.13) ; extra == 'dev'
  31. Requires-Dist: pre-commit (==3.1.0) ; extra == 'dev'
  32. Requires-Dist: tox ; extra == 'dev'
  33. Provides-Extra: docs
  34. Requires-Dist: sphinx (==6.1.3) ; extra == 'docs'
  35. Requires-Dist: alabaster (==0.7.13) ; extra == 'docs'
  36. Requires-Dist: sphinx-issues (==3.0.1) ; extra == 'docs'
  37. Provides-Extra: lint
  38. Requires-Dist: flake8 (==6.0.0) ; extra == 'lint'
  39. Requires-Dist: flake8-bugbear (==23.2.13) ; extra == 'lint'
  40. Requires-Dist: pre-commit (==3.1.0) ; extra == 'lint'
  41. Provides-Extra: tests
  42. Requires-Dist: pytest ; extra == 'tests'
  43. Requires-Dist: pytest-lazy-fixture (>=0.6.2) ; extra == 'tests'
  44. **********************
  45. marshmallow-sqlalchemy
  46. **********************
  47. |pypi-package| |build-status| |docs| |marshmallow3| |black|
  48. Homepage: https://marshmallow-sqlalchemy.readthedocs.io/
  49. `SQLAlchemy <http://www.sqlalchemy.org/>`_ integration with the `marshmallow <https://marshmallow.readthedocs.io/en/latest/>`_ (de)serialization library.
  50. Declare your models
  51. ===================
  52. .. code-block:: python
  53. import sqlalchemy as sa
  54. from sqlalchemy.ext.declarative import declarative_base
  55. from sqlalchemy.orm import scoped_session, sessionmaker, relationship, backref
  56. engine = sa.create_engine("sqlite:///:memory:")
  57. session = scoped_session(sessionmaker(bind=engine))
  58. Base = declarative_base()
  59. class Author(Base):
  60. __tablename__ = "authors"
  61. id = sa.Column(sa.Integer, primary_key=True)
  62. name = sa.Column(sa.String, nullable=False)
  63. def __repr__(self):
  64. return "<Author(name={self.name!r})>".format(self=self)
  65. class Book(Base):
  66. __tablename__ = "books"
  67. id = sa.Column(sa.Integer, primary_key=True)
  68. title = sa.Column(sa.String)
  69. author_id = sa.Column(sa.Integer, sa.ForeignKey("authors.id"))
  70. author = relationship("Author", backref=backref("books"))
  71. Base.metadata.create_all(engine)
  72. Generate marshmallow schemas
  73. ============================
  74. .. code-block:: python
  75. from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
  76. class AuthorSchema(SQLAlchemySchema):
  77. class Meta:
  78. model = Author
  79. load_instance = True # Optional: deserialize to model instances
  80. id = auto_field()
  81. name = auto_field()
  82. books = auto_field()
  83. class BookSchema(SQLAlchemySchema):
  84. class Meta:
  85. model = Book
  86. load_instance = True
  87. id = auto_field()
  88. title = auto_field()
  89. author_id = auto_field()
  90. You can automatically generate fields for a model's columns using `SQLAlchemyAutoSchema`.
  91. The following schema classes are equivalent to the above.
  92. .. code-block:: python
  93. from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
  94. class AuthorSchema(SQLAlchemyAutoSchema):
  95. class Meta:
  96. model = Author
  97. include_relationships = True
  98. load_instance = True
  99. class BookSchema(SQLAlchemyAutoSchema):
  100. class Meta:
  101. model = Book
  102. include_fk = True
  103. load_instance = True
  104. 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.
  105. (De)serialize your data
  106. =======================
  107. .. code-block:: python
  108. author = Author(name="Chuck Paluhniuk")
  109. author_schema = AuthorSchema()
  110. book = Book(title="Fight Club", author=author)
  111. session.add(author)
  112. session.add(book)
  113. session.commit()
  114. dump_data = author_schema.dump(author)
  115. print(dump_data)
  116. # {'id': 1, 'name': 'Chuck Paluhniuk', 'books': [1]}
  117. load_data = author_schema.load(dump_data, session=session)
  118. print(load_data)
  119. # <Author(name='Chuck Paluhniuk')>
  120. Get it now
  121. ==========
  122. ::
  123. pip install -U marshmallow-sqlalchemy
  124. Requires Python >= 3.7, marshmallow >= 3.0.0, and SQLAlchemy >= 1.3.0.
  125. Documentation
  126. =============
  127. Documentation is available at https://marshmallow-sqlalchemy.readthedocs.io/ .
  128. Project Links
  129. =============
  130. - Docs: https://marshmallow-sqlalchemy.readthedocs.io/
  131. - Changelog: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/changelog.html
  132. - Contributing Guidelines: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/contributing.html
  133. - PyPI: https://pypi.python.org/pypi/marshmallow-sqlalchemy
  134. - Issues: https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues
  135. License
  136. =======
  137. MIT licensed. See the bundled `LICENSE <https://github.com/marshmallow-code/marshmallow-sqlalchemy/blob/dev/LICENSE>`_ file for more details.
  138. .. |pypi-package| image:: https://badgen.net/pypi/v/marshmallow-sqlalchemy
  139. :target: https://pypi.org/project/marshmallow-sqlalchemy/
  140. :alt: Latest version
  141. .. |build-status| image:: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml/badge.svg
  142. :target: https://github.com/marshmallow-code/marshmallow-sqlalchemy/actions/workflows/build-release.yml
  143. :alt: Build status
  144. .. |docs| image:: https://readthedocs.org/projects/marshmallow-sqlalchemy/badge/
  145. :target: http://marshmallow-sqlalchemy.readthedocs.io/
  146. :alt: Documentation
  147. .. |marshmallow3| image:: https://badgen.net/badge/marshmallow/3
  148. :target: https://marshmallow.readthedocs.io/en/latest/upgrading.html
  149. :alt: marshmallow 3 compatible
  150. .. |black| image:: https://badgen.net/badge/code%20style/black/000
  151. :target: https://github.com/ambv/black
  152. :alt: code style: black