question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Partial loading on nested fields

See original GitHub issue

Given 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Finessecommented, Aug 13, 2020

Wasn’t it fixed in #849?

@lafrech It hasn’t covered a case like this:

class Author(Schema):
    first_name = fields.Str(required=True)
    last_name = fields.Str(required=True)

class Book(Schema):
    author = fields.Nested(Author(partial=True), required=True)

Book().load({"author": {"last_name": "King"}}) # Undesired error

Here is my workaround for this case:

def partialSchema(schema_cls, **kwargs):
    schema = schema_cls(partial=True, **kwargs)
    for field in schema.fields.values():
        # This call doesn't make a side affect of changing the field object defined in the schema class
        field.required = False
    return schema

class Author(Schema):
    first_name = fields.Str(required=True)
    last_name = fields.Str(required=True)

class Book(Schema):
    author = fields.Nested(partialSchema(Author), required=True)

Book().load({"author": {"last_name": "King"}}) # No error
1reaction
Aeroncommented, Apr 10, 2020

I believe such a crucial caveat must be mentioned explicitly in the Schema documentation, regarding the partial parameter, because logically this parameter should work the same way even for a Schema instance wrapped by the Nested 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found