Could you help me to update nested objects in Flask-SQLAlchemy-Marshmallow. Here is the model
Parent Model
class ParentModel(db.Model):
__tablename__ = "parent"
id = db.Column(db.Integer, primary_key=True)
parent_name = db.Column(db.String(100), nullable=False)
childrens = db.relationship("ChildrensModel", cascade="all,delete", backref='parent')
@classmethod
def find_by_id(cls, _id: int) -> "ParentModel":
return cls.query.filter_by(id=_id).first()
@classmethod
def find_by_name(cls, name: int) -> "ParentModel":
return cls.query.filter_by(parent_name=name).first()
@classmethod
def find_all(cls) -> List["ParentModel"]:
return cls.query.all()
def save_to_db(self) -> None:
db.session.add(self)
db.session.commit()
def delete_from_db(self) -> None:
db.session.delete(self)
db.session.commit()
Children Model
class ChildrenModel(db.Model):
__tablename__ = "children"
id = db.Column(db.Integer, primary_key=True)
children_name = db.Column(db.String(100), nullable=False)
parent_id = db.Column(db.Integer,db.ForeignKey("parent.id") primary_key=True)
@classmethod
def find_by_id(cls, _id: int) -> "ChildrenModel":
return cls.query.filter_by(id=_id).first()
@classmethod
def find_by_name(cls, name: int) -> "ChildrenModel":
return cls.query.filter_by(children_name=name).first()
@classmethod
def find_all(cls) -> List["ChildrenModel"]:
return cls.query.all()
def save_to_db(self) -> None:
db.session.add(self)
db.session.commit()
def delete_from_db(self) -> None:
db.session.delete(self)
db.session.commit()
Children Schema
class ChildrenSchema(ma.ModelSchema):
class Meta:
model = ChildrenModel
include_fk = True
Parent Schema
class ParentSchema(ma.ModelSchema):
children= fields.List(fields.Nested(ChildrenSchema(dump_only=("parent_id",))),required=True)
class Meta:
model = ParentModel
include_fk = True
Here is the required JSON format. Could you please show me how to Update the existing nested object in following format.
{
"parent_name":"parent1",
"children":[{"children_name":"children_1"},{"children_name":"children_2"}]
}
And guys thankyou for your time.