Reputation: 639
I am using flask, sqlalchemy and marshmallow, I have a table with data in place and POST, GET and DELETE work fine, I am having a problem to update the entire row by passing data as JSON, I am querying the row by customer column, tried all options that I could find, no real example in updating data from JSON, any help?
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Feedback(db.Model):
id = db.Column(db.Integer, primary_key=True)
customer = db.Column(db.String(200), unique=True)
dealer = db.Column(db.String(200))
rating = db.Column(db.Integer)
comments = db.Column(db.Text)
def __init__(self, customer, dealer, rating, comments):
self.customer = customer
self.dealer = dealer
self.rating = rating
self.comments = comments
class FeedbackSchema(ma.Schema):
class Meta:
fields = ('id', 'customer', 'dealer', 'rating', 'comments')
feedback_schema = FeedbackSchema()
feedbacks_schema = FeedbackSchema(many=True)
@app.route('/updateReview', methods=['PUT'])
def updateReview():
customer = request.args['customer']
if customer == '':
return 'Bad request, please enter Customer', 404
customer = request.json['customer']
dealer = request.json['dealer']
rating = request.json['rating']
comments = request.json['comments']
update_customer = Feedback(customer, dealer, rating, comments)
cus = Feedback.query.filter_by(customer=customer).first()
cus.update(update_customer)
db.session.commit()
return 'ok'
Upvotes: 0
Views: 1533
Reputation: 434
The problem is in the way you are creating the update_customer
and way you are trying to update the cus
.
Try this one.
feedback = Feedback.query.filter_by(customer=customer).first()
feedback.customer = customer
feedback.dealer = dealer
feedback.rating = rating
feedback.comments = comments
db.session.add(feedback)
db.session.commit()
Upvotes: 3