Optional nesting (handling an embed parameter)
See original GitHub issueI’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:
- Created 9 years ago
- Comments:7 (3 by maintainers)
Top 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 >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
Fixed by #468.
Version 2.8.0 added support for specifying nested field names to the
only
andexclude
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