5: Validating APIs Using marshmallow
Activity 8: Serializing the recipe Object Using marshmallow
Solution
- Modify the recipe schema to include all attributes except for
email
. Inschemas/recipe.py
, modifyonly=['id', 'username'] to exclude=('email', )
. This way, we will be showing everything except for the user's email address. Besides, if we have a new attribute for therecipe
object in the future (for example, auser avatar
URL), we won't need to modify the schema again because it will show everything:Â Â Â Â Â author = fields.Nested(UserSchema, attribute='user', dump_only=True, exclude=('email', ))
- Modify the
get
method inRecipeResource
to serialize therecipe
object into JSON format using the recipe schema:Â Â Â Â Â Â Â Â return recipe_schema.dump(recipe).data, HTTPStatus.OK
This is mainly to modify the code to use
recipe_schema.dump(recipe).data
to return...