Question: how is the "only" attribute of a schema intended to be used?
See original GitHub issueFrom the marshmallow docs:
only (tuple) – A list or tuple of fields to serialize. If None, all fields will be serialized. Nested fields can be represented with dot delimiters.
This gives me the impression that it is a separate attribute from fields that is used on serialization to know which fields to serialize. This is partly true in that if only
is set on a schema instance and dump is called, it does indeed affect which fields are serialized. But if you look at the schema instance after dump is called, it actually has “destructively” modified the fields
attribute of the schema. From my perspective, it seems as though fields
should remain unchanged – only
should merely inform the serialization process during the execution of the serialization as opposed to altering the schema instance. I am possibly misunderstanding how it was intended to be used, but the docs may need to be clarified in that case.
A little example to further explain my use case and why I hoped it would behave the way I imagined (using marshmallow with Flask):
from schemas import SchemaClass
@app.route('/collection/<resource_id>')
def route_handler(**kwargs):
return getattr(ViewHelperClass, request.method.lower())(**kwargs)
class ViewHelperClass:
@staticmethod
@SchemaClass(many=False, exclude=['field1'], dump_only=['field2', 'field3'])
def get(**kwargs):
# Do some stuff
# return something
In my case, I’ve provided a base schema which all other schemas (including SchemaClass
in the above example) that extends the functionality a bit to allow an instance of a schema to decorate a function. This is highly useful, because I know all the attributes of the schema instance prior to execution time.
However, I allow the client to request only certain fields be returned: api.example.com/collection/1?fields=field3,field4
. The only way to satisfy that request is by setting the only
attribute of the schema. This has some unfortunate side effects, though, because the schema instance is not recycled for the next request, meaning the fields have been permanently altered for future requests when dump
is called with a subset of fields supplied to only
.
Sorry for the long explanation, but hopefully that helps clarify the issue I’m having. To reiterate my question: was only
to be used exclusively on one-off instances of schemas? If that is the case and to remain the case, would it be possible to provide an argument for serialization that allows specifying this? For example, schema_inst.dump(data, only=only_the_fields_I_want)
?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:15 (8 by maintainers)
Top GitHub Comments
No problem, you’re welcome. I was just trying to understand. Glad you’re not stuck.
@justin-richert, considering your use case, you may be interested by https://github.com/marshmallow-code/marshmallow/issues/783 and https://github.com/marshmallow-code/marshmallow/issues/785.