Search code examples
python-3.xsqlalchemyflask-restfulmarshmallow

AttributeError: 'dict' object has no attribute '_sa_instance_state'


Am creating a flask_restful API with sqlalchemy and marshmallow. Here is my model

class User(db.Model):
    """Basic user model
    """

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    active = db.Column(db.Boolean, default=True)
    is_admin = db.Column(db.Boolean, default=False)
    profile = db.relationship("Profile", back_populates="user", uselist=False)

class Profile(db.Model):
    """Profile model
    """

    id = db.Column(db.Integer, primary_key=True)
    fullname = db.Column(db.String(80), unique=False, nullable=True)
    img_url = db.Column(db.String(255), unique=False, nullable=True)
    telephone = db.Column(db.String(20), unique=False, nullable=True)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('user.id'),
        nullable=False)
    user = db.relationship("User", back_populates="profile")

and here user resource for updating PUT request

class UserResource(Resource):

    def put(self, user_id):
        schema = UserSchema()
        user = User.query.get_or_404(user_id)

        data = {
            'username': 'updated',
            "password": 'HRcdxRu45',
            'email': 'ms@gt.vom',
            'profile': {
                'telephone': '+2507800112233',
                'fullname': 'Mic Lolo',
                'img_url': 'images/img.jpg'
            }
        }

        user = schema.load(data, instance=user)

        return {"message": "user updated", "data": schema.dump(user)}

Here is my schema

class ProfileSchema(ma.Schema):
    id = ma.Int(dump_only=True)
    fullname = ma.Str(validate=Length(min=3, max=80, error='empty fullname'))
    img_url = ma.Str(validate=Length(min=3, max=80, error='empty image url'))
    telephone = ma.Str(validate=Regexp(regex=r"\+[0-9]{7,13}", error="Invalid phone number"))

    class Meta:
        model = Profile
        sqla_session = db.session


class UserSchema(ma.ModelSchema):
    id = ma.Int(dump_only=True)
    password = ma.Str(load_only=True,
                      validate=Length(min=8, max=255, error='empty password'))
    username = ma.Str(validate=Length(min=3, max=80, error='empty username'))
    is_admin = ma.Bool()
    email = ma.Email()
    profile = ma.Nested(ProfileSchema)

    class Meta:
        model = User
        sqla_session = db.session

Am geting AttributeError: 'dict' object has no attribute '_sa_instance_state'

When I remove the nested profile schema from UserSchema there is no error but user.profile will not be updated. Also when I remove the instance=user parameter in schema.load(data, instance=user) there is no error and I will get a fine user instance with posted data the problem here again is that there will not be any link between that user instance and the one existing in the database, I will have to find manually which user's field to update.


Solution

  • class ProfileSchema(ma.Schema):
    

    ProfileSchema should inherit from ma.ModelSchema