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.

Optional nesting (handling an embed parameter)

See original GitHub issue

I’m aware of how to nest items, as described in the documentation. Per those examples (with slight modifications) I am able to nest, say, a Author in a Book:

class AuthorSchema(Schema):
    class Meta:
        fields = ('id', 'name')

class BookSchema(Schema):
    author = fields.Nested(AuthorSchema, only='id')
    class Meta:
        fields = ('id', 'title', 'author')

This is all fine and dandy. However, say I was exposing these models over a REST API and I wanted to implement an embed paremeter, as described here. From what I can see, I would need to either (a) define a huge number of alternative schemas (i.e. BookSchema_AuthorID, BookSchema_AuthorName, BookSchema_AuthorIDName etc.) or (b) perform some processing on the serialized output and strip out fields I don’t want. Neither of these are ideal. Is there any alternative?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
deckar01commented, Jun 24, 2016

Fixed by #468.

0reactions
deckar01commented, Jun 24, 2016

Version 2.8.0 added support for specifying nested field names to the onlyand exclude options on your schema constructors. This allows you to spin up deeply customized schema instances without declaring new schema classes or reaching into undocumented parts of the schema interface.

Check out the documentation for the dotted nesting syntax in the “Nesting Schema’s” section of the docs.

https://marshmallow.readthedocs.io/en/latest/nesting.html#specifying-which-fields-to-nest

class SiteSchema(Schema):
    blog = fields.Nested(BlogSchema2)

schema = SiteSchema(only=['blog.author.email'])
result, errors = schema.dump(site)
pprint(result)
# {
#     'blog': {
#         'author': {'email': u'monty@python.org'}
#     }
# }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Java 8 Optional - how to handle nested Object structures
Is there any easy way to make this code short and sweet in Java 8? import java.util.Optional; public class OptionalInnerStruct { public static ......
Read more >
Bug? LaTeX misparses nested optional arguments - TeX
LaTeX seems to be having problems handling nested optional arguments. I've included a minimal code example that generates the error:
Read more >
REST API Design Best Practices for Sub and Nested Resources
The parameters are optional, so we could also use it to get all contributions, and we can PUT and POST to it to...
Read more >
Listings of nested sub-resources - Medium
Managing nested resources & lists of nested sub-resources in a REST API ... Therefore, we should add an optional query parameter to all ......
Read more >
Nesting Schemas — marshmallow 3.19.0 documentation
Dotted paths may be passed to only and exclude to specify nested attributes. class SiteSchema(Schema): blog = fields ...
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