D
D
domanskiy2020-10-16 13:33:26
Flask
domanskiy, 2020-10-16 13:33:26

How to achieve automatic generation of Shema models using Marshmallow and SQLAlchemyAutoSchema?

It seems to be acting according to the documentation here

. There is a model and a diagram:

class TypeMaterial(db.Model):
    __tablename__ = 'types_material'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), unique=True)
    description = db.Column(db.String(255))

    def __repr__(self):
        return f'<ICC Profile: {self.name}>'

# Исплользую  SQLAlchemyAutoSchema для авто-генерации схемы
class TypeMaterialSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = TypeMaterial
        load_instance = True


In blueprint, I try to make a GET request, output to the json database console
@api.route('/')
@login_required
def index():
    items = TypeMaterial.query().all()
    result = TypeMaterialSchema.dump(items)
    pprint(result)


Gives an error to the console:
File "E:\MyPyProjects\flask\app\api\blueprint.py", line 45, in index
result = TypeMaterialSchema.dump(items)
TypeError: dump() missing 1 required positional argument: 'obj'


What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
domanskiy, 2020-10-16
@domanskiy

Found a solution

items = TypeMaterial.query.all()
    items_schema = TypeMaterialSchema(many=True)
    results = items_schema.dump(items)
    print(results)
    return jsonify(results)

It seems to me very convenient to use SQLAlchemyAutoSchema
Maybe someone will write if he has any pitfalls?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question