-
-
Notifications
You must be signed in to change notification settings - Fork 158
Recipes
Steven Loria edited this page Mar 7, 2017
·
2 revisions
Various recipes and patterns for webargs.
For 0.16.0 onwards, use of marshmallow is encouraged. Marshmallow brings a whole slew of serialization/deserialization features, which is exactly what you're doing when you take web requests from various frameworks and turn them into native python data types: deserializing.
use=
used to let you change the value of a field while it was parsed. You can accomplish that and more now with marshmallow's post_load
decorator:
from marshmallow import Schema, post_load
from webargs import fields
class ArgSchema(Schema):
name = fields.Str(required=True, location='querystring')
@post_load
def uppercase_name(self, data):
data['name'] = data['name'].upper()
return data
See the documentation for how to use marshmallow schemas in webargs.
Since you now have access to all fields in the data
argument of your post_load
method, you can do cross preprocessing, meaning you can change the value of one field based upon the value of another.
Credit: @dwieeb