Skip to content

How is deserialization affected by only and partial? #535

Open
@redlamb

Description

@redlamb

I was hoping to get some clarification on the differences between only and partial with respect to deserialization. (This question is similar to #509 which addresses serialization using the same fields.) Based on the docs and the response to that issue, I interpreted it to mean that only applies to serialization and partial applies to deserialization. However, in testing it looks like only also affects deserialization. What am I missing?

from marshmallow import Schema, fields

class MySchema(Schema):
    foo = fields.Field(required=True)
    bar = fields.Field(required=True)

only_schema = MySchema(only=('foo', ))
partial_schema = MySchema(partial=True)

print(only_schema.load({'foo': 42, 'bar': 24}))
print(only_schema.load({'foo': 42}))
print(partial_schema.load({'foo': 42, 'bar': 24}))
print(partial_schema.load({'foo': 42}))

Output:

UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={u'foo': 42}, errors={})
UnmarshalResult(data={'foo': 42, 'bar': 24}, errors={})
UnmarshalResult(data={'foo': 42}, errors={})

I expected deserialization using only_schema to result in an error similar to the example below.

MySchema().load({'foo':42})

Output:

UnmarshalResult(data={'foo': 42}, errors={'bar': [u'Missing data for required field.']})

Ultimately, I'm trying to get away with using the same schema for (de)serialization, but maybe that's not the ideal way to use marshmallow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions