How to use Update query in Flask Peewee? How to use Update query in Flask Peewee? flask flask

How to use Update query in Flask Peewee?


First, it looks like you are using pre-2.0 syntax (the filter method is now deprecated). I'd recommend looking at the docs for info on the latest version.

Typically, you do not "update a query". The two main ways of accomplishing this is are...

1.) Use a query to retrieve an object, then use the save() method to update the object. For example...

mdetails = MerchantDetails.select().where(MerchantDetails.id == 42).get()mdetails.name = 'new name'mdetails.save() # Will do the SQL update query.

2.) Use a SQL update statement...

q = MerchantDetails.update(MerchantDetails.name='new name')    .where(MerchantDetails.id == 42)q.execute() # Will do the SQL update query.

Both of these, in essence, accomplish the same thing. The first will make two queries o the database (one to SELECT the record, another to UPDATE the record), while the second will only use one SQL call (to UPDATE the record).


I got the solution

mdetails = merchant_details.update(          merchant_name=request.form['merchantname'],          first_name=request.form['firstname'],          last_name=request.form['lastname'],          street_1=request.form['street1'],          street_2=request.form['street2'],          state=request.form['state'],          city=request.form['city'],          phone=request.form['phone'],          zipcode=request.form['zip'],        ).where(merchant_details.merchant_id==session['userid'])        mdetails.execute()

Anyways Thanks Mark


I searched for this solution too and thanks to @Mark and @Rohit I changed my code (peeweee with PostgreSQL) and is working.

To add a small improve it seems the update will be executed even if you will not use the variable. For me is simpler and a cleaner code:

merchant_details.update(      merchant_name=request.form['merchantname'],      first_name=request.form['firstname'],      last_name=request.form['lastname'],      street_1=request.form['street1'],      street_2=request.form['street2'],      state=request.form['state'],      city=request.form['city'],      phone=request.form['phone'],      zipcode=request.form['zip'],    ).where(merchant_details.merchant_id==session['userid']).execute()