Partial loading on nested fields
See original GitHub issueGiven an object with a nested field, is it possible to partially load the object and all nested fields? e.g.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from marshmallow import Schema, fields
class Address(Schema):
street = fields.Str(required=True)
city = fields.Str(required=True)
state = fields.Str(required=True)
zipcode = fields.Str(required=True)
country = fields.Str(required=True)
class User(Schema):
first_name = fields.Str(required=True)
last_name = fields.Str(required=True)
address = fields.Nested(Address)
payload = {
'first_name': 'John',
'address': {
'zipcode': '90210'
}
}
user_schema = User()
data, errors = user_schema.load(payload, partial=True)
if errors:
raise Exception(errors)
Output:
Traceback (most recent call last):
File "test_partial_nested.py", line 30, in <module>
raise Exception(errors)
Exception: {'address': {'city': ['Missing data for required field.'], 'state': ['Missing data for required field.'], 'street': ['Missing data for required field.'], 'country': ['Missing data for required field.']}}
Input is coming from a REST API and ideally I would like to bind all incoming payload at once.
Thanks!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Partial loading on nested fields · Issue #438 - GitHub
Given an object with a nested field, is it possible to partially load the object and all nested fields? e.g. #!
Read more >Nesting Schemas — marshmallow 3.19.0 documentation
Partial Loading ¶ ... Nested schemas also inherit the partial parameter of the parent load call. ... You can specify a subset of...
Read more >Use nested Schema with allow_blank in marshmallow
I'd like to use marshmallow to validate a structure with a nested schema. Here is an example: from marshmallow import Schema, fields class ......
Read more >marshmallow -- simplified object serialization | We all are data.
Partial Loading. Nested schemas also inherit the partial parameter of the parent load call. class UserSchemaStrict(Schema): name = fields.
Read more >marshmallow - Read the Docs
Use a Nested field to represent the relationship, passing in a nested schema class. from marshmallow import Schema, fields, pprint class UserSchema(Schema):.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lafrech It hasn’t covered a case like this:
Here is my workaround for this case:
I believe such a crucial caveat must be mentioned explicitly in the
Schema
documentation, regarding thepartial
parameter, because logically this parameter should work the same way even for aSchema
instance wrapped by theNested
field.Perhaps any
Nested
-related documentation also would be a suitable place for such a warning.At the moment, this issue, except for source code digging, is the only way a developer may learn this parameter works like that.