Creating schemas to validate, serialize, and deserialize models

Now, we will create the Flask-Marshmallow schemas that we will use to validate, serialize, and deserialize the previously declared NotificationCategory and Notification models, and their relationships.

Open the file within the service folder and add the following code after the last line. The lines that declare fields related to other schemas are highlighted in the code listing. The code file for the sample is included in the restful_python_2_02_01 folder, in the Flask01/service/ file:

class NotificationCategorySchema(ma.Schema): id = fields.Integer(dump_only=True) # Minimum length = 3 characters name = fields.String(required=True, validate=validate.Length(3)) ...

Get Hands-On RESTful Python Web Services - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.