UD - FLASK - Serialization
why serialization
easier serialization / deserialization
separate model from data interaction
whitelist params
json & reqparse are no longer required.
1. Vanilla marshmallow
core
from marshmallow import Schema, fields
class BookSchema(Schema):
title = fields.Str()
author = fields.Str()
class Book:
def __init__(self, title, author, description):
self.title = title
self.author = author
self.description = description1.1. Serialization -> dict
1.2. Deserialization -> object
2. Flask-marshmallow
schemawill loadjsontoobjectdirectly.tight integration between schema & model;
fieldsonly declare in one place.init&jsonmethods are no longer required inmodel.reqparsemethod is no longer required in resource.
2.1. Components
model (no marshmallow here)
schema (fields are defined by models)
resource
2.2. Nested schema
Last updated
Was this helpful?