marshmallow-sqlalchemy icon indicating copy to clipboard operation
marshmallow-sqlalchemy copied to clipboard

How custom deserialize for '@property' attribute?

Open gotounix opened this issue 4 years ago • 1 comments

class Test(Model):
    ...
    
    @property
    def my_user(self):
        return User.query.filter_by(id=1).first()
class TestSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Node
        include_fk = True
        unknown = INCLUDE
    my_user = ma.Nested('UserSchema')

gotounix avatar Jun 10 '20 08:06 gotounix

I assume you only implemented a getter for your property as defined in your example. Personally, I just use a simple Marshmallow field with dump_only=True:

from marshmallow_sqlalchemy.fields import Nested

class TestSchema(ma.SQLAlchemyAutoSchema):

    # ...

    my_user = Nested('UserSchema', dump_only=True)

3j14 avatar Jun 23 '20 10:06 3j14