New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question/docs suggestion: Context for deserialization #224

Closed
martinstein opened this Issue Jun 17, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@martinstein

martinstein commented Jun 17, 2015

The docs currently mention the context attribute, but only for serializing values:
http://marshmallow.readthedocs.org/en/latest/custom_fields.html#adding-context-to-method-and-function-fields

Let's say I want to deserialize some data and have a validation method that additionally depends on the currently logged in user-object. Is this the proper way to pass the user into the schema?

schema.context = {'user': user}
data, errors = schema.load(input_data)

If yes, perhaps it would be nice to have another, tiny section in the docs that mentions that context can also be used for deserialization (currently it looks like context is for serialization only).

@sloria

This comment has been minimized.

Member

sloria commented Jun 17, 2015

Yes, that is the correct usage. I am open to adding more documentation to clarify that context can be used in deserialization. PRs welcome!

@martinstein

This comment has been minimized.

martinstein commented Jun 17, 2015

Okay, thanks for the fast reply. By the way, I really like the implicit field (class Meta: fields = (...)) and nesting functionality. Those two features in combination are great for serializing SqlAlchemy-models.

@sloria

This comment has been minimized.

Member

sloria commented Jun 18, 2015

Good to hear. Since you're using SQLAlchemy, I recommend using marshmallow-sqlalchemy; it will more reliably and efficiently generate Schemas based on models.

@sloria sloria closed this in 481453b Aug 24, 2015

@sloria

This comment has been minimized.

Member

sloria commented Aug 24, 2015

Added a brief section that explains that context is available to Schema and Field methods. Closing this for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment