DESCRIPTION.rst 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. .. image:: https://travis-ci.org/MongoEngine/marshmallow-mongoengine.svg?branch=master
  2. :target: https://travis-ci.org/MongoEngine/marshmallow-mongoengine
  3. :alt: Travis-CI
  4. .. image:: https://readthedocs.org/projects/marshmallow-mongoengine/badge/?version=latest
  5. :target: http://marshmallow-mongoengine.readthedocs.org/en/latest/?badge=latest
  6. :alt: Documentation Status
  7. .. image:: https://coveralls.io/repos/github/MongoEngine/marshmallow-mongoengine/badge.svg?branch=master
  8. :target: https://coveralls.io/github/MongoEngine/marshmallow-mongoengine?branch=master
  9. :alt: Code Coverage
  10. marshmallow-mongoengine
  11. =======================
  12. `Mongoengine <http://mongoengine.org>`_ integration with the `marshmallow <https://marshmallow.readthedocs.org/en/latest/>`_ (de)serialization library (`toastedmarshamallow <https://pypi.python.org/pypi/toastedmarshmallow>`_ also supported).
  13. See documentation at http://marshmallow-mongoengine.rtfd.org/
  14. Declare your models
  15. -------------------
  16. .. code-block:: python
  17. import mongoengine as me
  18. class Author(me.Document):
  19. id = me.IntField(primary_key=True, default=1)
  20. name = me.StringField()
  21. books = me.ListField(me.ReferenceField('Book'))
  22. def __repr__(self):
  23. return '<Author(name={self.name!r})>'.format(self=self)
  24. class Book(me.Document):
  25. title = me.StringField()
  26. Generate marshmallow schemas
  27. ----------------------------
  28. .. code-block:: python
  29. from marshmallow_mongoengine import ModelSchema
  30. class AuthorSchema(ModelSchema):
  31. class Meta:
  32. model = Author
  33. class BookSchema(ModelSchema):
  34. class Meta:
  35. model = Book
  36. author_schema = AuthorSchema()
  37. (De)serialize your data
  38. -----------------------
  39. .. code-block:: python
  40. author = Author(name='Chuck Paluhniuk').save()
  41. book = Book(title='Fight Club', author=author).save()
  42. dump_data = author_schema.dump(author).data
  43. # {'id': 1, 'name': 'Chuck Paluhniuk', 'books': ['5578726b7a58012298a5a7e2']}
  44. author_schema.load(dump_data).data
  45. # <Author(name='Chuck Paluhniuk')>
  46. Get it now
  47. ----------
  48. ::
  49. pip install -U marshmallow-mongoengine
  50. License
  51. -------
  52. MIT licensed. See the bundled `LICENSE <https://github.com/touilleMan/marshmallow-mongoengine/blob/master/LICENSE>`_ file for more details.