Ramin Farajpour Cami Ramin Farajpour Cami - 2 months ago 10
Python Question

deserialize json model in model with marshmallow python

I going to deserialize json model

class LogsSchema(ModelSchema):
class Meta:
model = Logs
sqla_session = Session

class UsersSchema(ModelSchema):
logs = fields.Nested(LogsSchema,exclude=('User', ))
class Meta:
model = Users
sqla_session = Session


json model request

<QueryDict: {u'Username': [u'ramin world'], u'logs': [u'[UserLog=test]'], u'Date': [u'null'], u'Password': [u'1234'], u'Email': [u'ramin@blacat.gmail.com'], u'Name': [u'Farajpour']}>


load json request:

users_schema = UsersSchema()
json_dict = users_schema.load(usermodel,session=Session).data


result:

{'Username': u'ramin world', 'Password': u'1234', 'Name': u'Farajpour', 'Email': u'ramin@blacat.gmail.com'}


you see model
u'logs': [u'[UserLog=test]']
is not on result.

Answer

Try add many=Truein logs:

class UsersSchema(ModelSchema):
    logs = fields.Nested(LogsSchema,exclude=('User', ), many=True)
    ...
Comments