https://github.com/mathewmarcus/marshmallow-pynamodb
PynamoDB integration with marshmallow
https://github.com/mathewmarcus/marshmallow-pynamodb
api dynamodb json marshmallow nosql pynamodb python
Last synced: 5 months ago
JSON representation
PynamoDB integration with marshmallow
- Host: GitHub
- URL: https://github.com/mathewmarcus/marshmallow-pynamodb
- Owner: mathewmarcus
- License: mit
- Created: 2017-02-05T20:59:45.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-06-01T22:53:30.000Z (about 5 years ago)
- Last Synced: 2025-09-25T01:22:05.848Z (9 months ago)
- Topics: api, dynamodb, json, marshmallow, nosql, pynamodb, python
- Language: Python
- Homepage:
- Size: 50.8 KB
- Stars: 9
- Watchers: 1
- Forks: 6
- Open Issues: 7
-
Metadata Files:
- Readme: README.rst
- License: LICENSE.txt
Awesome Lists containing this project
README
====================
marshmallow-pynamodb
====================
.. image:: https://badge.fury.io/py/marshmallow-pynamodb.svg
:target: http://badge.fury.io/py/marshmallow-pynamodb
:alt: Latest version
.. image:: https://travis-ci.org/mathewmarcus/marshmallow-pynamodb.svg?branch=master
:target: https://travis-ci.org/mathewmarcus/marshmallow-pynamodb
:alt: Travis-CI
`PynamoDB `_ integration with the `marshmallow `_ (de)serialization library.
Installation
============
From PyPi::
$ pip install marshmallow-pynamodb
From GitHub::
$ pip install git+https://github.com/mathewmarcus/marshmallow-pynamodb#egg=marshmallow_pynamodb
Declare your models
===================
.. code-block:: python
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
class User(Model):
class Meta:
table_name = "user"
email = UnicodeAttribute(null=True)
first_name = UnicodeAttribute(range_key=True)
last_name = UnicodeAttribute(hash_key=True)
Generate marshmallow schemas
============================
.. code-block:: python
from marshmallow_pynamodb import ModelSchema
class UserSchema(ModelSchema):
class Meta:
model = User
user_schema = UserSchema()
(De)serialize your data
=======================
.. code-block:: python
user = User(last_name="Smith", first_name="John")
user_schema.dump(user).data
# {u'first_name': u'John', u'last_name': u'Smith', u'email': None}
user_schema.load({"last_name": "Smith", "first_name": "John"}).data
# user
Nested models? No problem
=========================
.. code-block:: python
from marshmallow_pynamodb.schema import ModelSchema
from pynamodb.models import Model
from pynamodb.attributes import ListAttribute, MapAttribute, NumberAttribute, UnicodeAttribute
class Location(MapAttribute):
latitude = NumberAttribute()
longitude = NumberAttribute()
name = UnicodeAttribute()
class Person(MapAttribute):
firstName = UnicodeAttribute()
lastName = UnicodeAttribute()
age = NumberAttribute()
class OfficeEmployeeMap(MapAttribute):
office_employee_id = NumberAttribute()
person = Person()
office_location = Location()
class Office(Model):
class Meta:
table_name = 'OfficeModel'
office_id = NumberAttribute(hash_key=True)
address = Location()
employees = ListAttribute(of=OfficeEmployeeMap)
class OfficeSchema(ModelSchema):
class Meta:
model = Office
OfficeSchema().load({'office_id': 789,
'address': {'latitude': 6.98454,
'longitude': 172.38832,
'name': 'some_location'},
'employees': [{'office_employee_id': 123,
'person': {'firstName': 'John',
'lastName': 'Smith',
'age': 45},
'office_location': {'latitude': -24.0853,
'longitude': 144.87660,
'name': 'other_location'}},
{'office_employee_id': 456,
'person': {'firstName': 'Jane',
'lastName': 'Doe',
'age': 33},
'office_location': {'latitude': -20.57989,
'longitude': 92.30463,
'name': 'yal'}}]}).data
# Office<789>
License
=======
MIT licensed. See the bundled `LICENSE `_ file for more details.