I'm using marshmallow-sqlalchemy with flask extension and trying to use the load method of my ModelSchema class. I have something like this:
db = SQLAlchemy()
ma = Marshmallow()
#I'm using Application Factorie
def create_app():
...
db.init_app(app)
ma.init_app(app)
...
return app
class BaseSchema(ma.ModelSchema):
class Meta:
sqla_session = db.session
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(45), nullable=False)
child_id = db.Column(db.Integer, db.ForeignKey("child.id"), nullable=False)
child = db.relationship("Child")
class ParentSchema(BaseSchema):
class Meta:
model = Parent
child = ma.Nested("ChildSchema")
class Child(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(25), nullable=False)
class ChildSchema(BaseSchema):
class Meta:
model = Child
Overall works perfectly, queries, inserts... But when I try to load a new parent object with a existing child in data base like: new_parent = ParentSchema().load({'name':'Foo', 'child' : {'id':1,'name':'Bar'} })
returns it AttributeError: 'DummySession' object has no attribute 'query'
.
What am i doing wrong?
I THINK that your BaseSchema's Meta class isn't being inherited properly.
Change this
class ParentSchema(BaseSchema):
class Meta:
model = Parent
to this
class ParentSchema(BaseSchema):
class Meta(BaseSchema.Meta):
model = Parent
Reference: https://marshmallow-sqlalchemy.readthedocs.io/en/latest/recipes.html (Base Schema I example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With