Reputation: 493
I'm trying to implement a PATCH
route for the following SQLAlchemy model and Marshmallow-SQLAlchemy schema. I want to update description
, address
, or both.
When I send the only one field in the data and try to load it, Marshmallow says the other field is missing. How can I load patch data with Marshmallow without it validating missing fields?
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False, unique=True)
password = db.Column(db.String(80))
description = db.Column(db.String(180))
address = db.Column(db.String(180))
class UserSchema(ma.ModelSchema):
class Meta:
model = UserModel
load_only = ("password",)
dump_only = ("id",)
{
"description": "New Description"
}
user_data = user_schema.load(request.get_json())
{
"username": [
"Missing data for required field."
]
}
Upvotes: 5
Views: 1920
Reputation: 1863
Since it is PATCH you can pass your model instance and flag partial=True
to the schema.
user_data = user_schema.load(request.get_json(), instance=instance, partial=True)
Upvotes: 7