我正在使用烧瓶,sqlalchemy和棉花糖,我有一个放置有数据的表,并且POST,GET和DELETE工作正常,我在通过将数据作为JSON传递来更新整个行时遇到问题,我正在查询行按客户列,尝试了所有可能找到的选项,没有从JSON更新数据的真实示例,有帮助吗?
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'
答案 0 :(得分:2)
问题出在创建update_customer
的方式以及试图更新cus
的方式上。
尝试这个。
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()