Package: marshmallow-pynamo-db
Welcome to Marshmallow-Pynamo-DBPynamoDBintegration with
theMarshmallow(de)serialization library.InstallationFrom PyPi:$pipinstallmarshmallow-pynamo-dbVersionsFor Marshmallow 3.x and PynamoDB 5.x use version1.xFor Marshmallow 3.x and PynamoDB 4.x use version0.10.6For Marshmallow 2.x and PynamoDB 3.x use original project:https://github.com/mathewmarcus/marshmallow-pynamodbDeclare your modelsfrompynamodb.modelsimportModelfrompynamodb.attributesimportUnicodeAttributeclassUser(Model):classMeta:table_name="user"email=UnicodeAttribute(null=True)first_name=UnicodeAttribute(range_key=True)last_name=UnicodeAttribute(hash_key=True)Generate marshmallow schemasfrommarshmallow_pynamodbimportModelSchemaclassUserSchema(ModelSchema):classMeta:model=Useruser_schema=UserSchema()(De)serialize your datauser=User(last_name="Smith",first_name="John")user_schema.dump(user)# {u'first_name': u'John', u'last_name': u'Smith', u'email': None}user_schema.load({"last_name":"Smith","first_name":"John"})# user<Smith>pynamodb-attributes supportCurrently we support the following custom attributes frompynamodb-attributeslibrary:IntegerAttribute– same asNumberAttributebut whose value is typed asint(rather thanfloat)UUIDAttribute- serializes aUUIDPython object as aStype attribute (e.g.'a8098c1a-f86e-11da-bd1a-00112444be1e')UnicodeEnumAttribute- serializes a string-valuedEnuminto a Unicode (S-typed) attributeIntegerEnumAttribute- serializes a integer-valuedEnuminto a
Number (S-typed) attributeimportuuidfromenumimportEnumfrompynamodb.attributesimportUnicodeAttributefrompynamodb.modelsimportModelfrompynamodb_attributesimportIntegerAttribute,UUIDAttribute,UnicodeEnumAttributefrommarshmallow_pynamodbimportModelSchemaclassGender(Enum):male="male"female="female"not_informed="not_informed"classPeople(Model):classMeta:table_name="people"uuid=UUIDAttribute(hash_key=True)first_name=UnicodeAttribute()last_name=UnicodeAttribute()gender=UnicodeEnumAttribute(Gender)age=IntegerAttribute()classPeopleSchema(ModelSchema):classMeta:model=Peoplepeople_schema=PeopleSchema()payload={"uuid":"064245dc0e5f415c95d3ba6b8f728ae4","first_name":"John","last_name":"Doe","gender":Gender.male.value,"age":43}people=people_schema.load(payload)# people<064245dc-0e5f-415c-95d3-ba6b8f728ae4>assertpeople.gender==Gender.maleassertpeople.uuid==uuid.UUID("064245dc0e5f415c95d3ba6b8f728ae4")See more examples in tests.Nested models? No problemfrommarshmallow_pynamodb.schemaimportModelSchemafrompynamodb.modelsimportModelfrompynamodb.attributesimport(ListAttribute,MapAttribute,NumberAttribute,UnicodeAttribute,)classLocation(MapAttribute):latitude=NumberAttribute()longitude=NumberAttribute()name=UnicodeAttribute()classPerson(MapAttribute):firstName=UnicodeAttribute()lastName=UnicodeAttribute()age=NumberAttribute()classOfficeEmployeeMap(MapAttribute):office_employee_id=NumberAttribute()person=Person()office_location=Location()classOffice(Model):classMeta:table_name='OfficeModel'office_id=NumberAttribute(hash_key=True)address=Location()employees=ListAttribute(of=OfficeEmployeeMap)classOfficeSchema(ModelSchema):classMeta:model=Office# noinspection PyTypeCheckerOfficeSchema().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'}}]})# Office<789>Using polyformed Models# pip install pynamodb_attributesimportuuidfrompynamodb_attributesimportUnicodeEnumAttribute,UUIDAttributefrompynamodb.attributesimportUnicodeAttribute,DiscriminatorAttributefrompynamodb.modelsimportModelfrommarshmallow_pynamodbimportModelSchemafromenumimportEnumclassMyStatus(Enum):CREATED="CREATED"classBaseDocument(Model):uuid=UUIDAttribute(default=uuid.uuid4)cls=DiscriminatorAttribute()classMyDocument(BaseDocument,discriminator='my_document'):status=UnicodeEnumAttribute(MyStatus,default=MyStatus.CREATED)content=UnicodeAttribute()classMyDocumentSchema(ModelSchema):classMeta:model=MyDocumentinstance=MyDocumentSchema().load({"content":"foo"})assertinstance.content=="foo"assertinstance.uuidisnotNoneLicenseMIT licensed. See the bundledLICENSEfile for more details.Not working?Dont panic. Get a towel and, please, open anissue.