Skip to main content

PynamoDB integration with the marshmallow (de)serialization library

Project description

Latest version Travis-CI

PynamoDB integration with the marshmallow (de)serialization library.


From PyPi:

$ pip install marshmallow-pynamodb

From GitHub:

$ pip install git+

Declare your models

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

from marshmallow_pynamodb import ModelSchema

class UserSchema(ModelSchema):
    class Meta:
        model = User

user_schema = UserSchema()

(De)serialize your data

user = User(last_name="Smith", first_name="John")

# {u'first_name': u'John', u'last_name': u'Smith', u'email': None}

user_schema.load({"last_name": "Smith", "first_name": "John"}).data
# user<Smith>

Nested models? No problem

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>


MIT licensed. See the bundled LICENSE file for more details.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for marshmallow-pynamodb, version 0.8.0
Filename, size File type Python version Upload date Hashes
Filename, size marshmallow-pynamodb-0.8.0.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page