To retire a field from the patient API we mark it load_only and dump_only at once: it should be accepted nowhere and returned nowhere, while the declaration stays in place for the migration. Dumping honours that. Loading…
To retire a field from the patient API we mark it load_only and dump_only at once: it should be accepted nowhere and returned nowhere, while the declaration stays in place for the migration. Dumping honours that. Loading does not: the field is still read from the input, validated and even enforced as required.
from marshmallow import Schema, fields
class UserSchema(Schema):
name = fields.String()
size = fields.Integer(load_only=True, dump_only=True)
UserSchema().load({"name": "Mick", "size": "12"})
ValidationError: {'size': ['Unknown field.']}
That is what any dump_only field gives when it shows up in the input.
{"name": "Mick", "size": 12}
unknown=EXCLUDE drops it and unknown=INCLUDE passes the raw value through), required=True on it demands nothing, and its missing default is not applied.Meta.load_only / Meta.dump_only, or with the load_only= / dump_only= arguments of the schema.Adapted from marshmallow-code/marshmallow (MIT).
Visible test command
pytest testsThe repository’s own tests. Run all tests includes them, and so does every submission.
Repository
load-only-and-dump-only-field-still-loads
Changes under src/ are evaluated.