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.

Deserializing only specific fields from a schema in fields.Nested?

See original GitHub issue

The only parameter for fields.Nested only applies to serialization.

Since you can only pass the schema class or name string to fields.Nested, there is currently no way to limit which fields are deserialized. Schema.__init__() allows use of dump_only and load_only, but these cannot currently be used with fields.Nested.

Is there a way to limit which fields are deserialized from a nested schema?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sloriacommented, Mar 19, 2017

I believe the desired behavior is already implemented–the only parameter is respected upon deserialization. I’ve verified that @joshfriend 's code example works in both marshmallow 2.13.3 and 3.0.0b1.

from marshmallow import Schema, fields

class IDSchema(Schema):
    id = fields.Integer()

class ChildSchema(IDSchema):
    name = fields.Str(allow_none=False, required=True)

class ParentSchema(Schema):
    # only load the ID of the nested objects
    children = fields.Nested(
        ChildSchema,
        many=True,
        required=True,
        load_only=True,
        only=('id',)
    )

    class Meta:
        strict = True


sch = ParentSchema()
data = sch.load({'children': [{'id': 1, 'name': 'foo'}]}).data
assert data == {'children': [{'id': 1}]}
0reactions
joshfriendcommented, Jan 14, 2016

I’m more interested in using only to ignore any extra info the client might send, rather than accepting however many fields the client wants to send. I think partial will already apply to nested schemas if it is given on the parent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting Schemas — marshmallow 3.19.0 documentation
Specifying Which Fields to Nest¶. You can explicitly specify which attributes of the nested objects you want to (de)serialize with the only argument...
Read more >
deserializing nested schema with only one exposed key
For anyone stumbling across the same issue, this is the workaround I am using currently: class MySchema(Schema): key = fields.
Read more >
Marshmallow Deserialize Nested Schema - Python Help
Hi everyone, I am trying to find a way to deserialize a json object using Marshmallow. ... class CustomerDTO(BaseDTO): first_name: str = fields....
Read more >
great_expectations.marshmallow__shade.schema
Metaclass for the Schema class. Binds the declared fields to a _declared_fields attribute, which is a dictionary mapping attribute names to field objects....
Read more >
How to use the marshmallow.fields.Method function in ... - Snyk
To help you get started, we've selected a few marshmallow.fields.Method examples, based on ... ModelSchema): reservation = Nested(ReservationSchema) state ...
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