0

From the docs under 'Deserializing to Objects':

from marshmallow import Schema, fields, post_load


class UserSchema(Schema):
    name = fields.Str()
    email = fields.Email()
    created_at = fields.DateTime()

    @post_load
    def make_user(self, data, **kwargs):
        return User(**data)

But I when I run this code, I get: AttributeError: 'User' object has no attribute 'data'

What am I missing?

Scott
  • 77
  • 1
  • 9
  • 1
    This code looks good to me. Please provide a complete example. Also please specify which marshmallow version you are using. – Jérôme Feb 20 '20 at 08:29

1 Answers1

1

Try returning the data dictionary instead of an instance of your model.

I stumbled upon issue this while using marshmallow-sqlalchemy and flask-marshmallow

from marshmallow import Schema, fields, post_load


class UserSchema(Schema):
    name = fields.Str()
    email = fields.Email()
    created_at = fields.DateTime()

    @post_load
    def make_user(self, data, **kwargs):
        data['extra_attribute'] = 'extra value'
        return data