Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Update query in Flask Peewee?

Hi I am using Flask Peewee and trying to update merchant_details model but it is not working. Following is the error I am getting:

AttributeError: 'SelectQuery' object has no attribute 'update'

mdetails = merchant_details.filter(merchant_details.merchant_id==session['userid']).update(
         merchant_name=request.form['merchantname'],
          first_name=request.form['firstname'],
          last_name=request.form['lastname'],
        )

Please Help!

like image 359
Rohit Dhiman Avatar asked Oct 08 '13 04:10

Rohit Dhiman


2 Answers

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(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).

like image 51
Mark Hildreth Avatar answered Sep 16 '22 22:09

Mark Hildreth


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

like image 40
Rohit Dhiman Avatar answered Sep 18 '22 22:09

Rohit Dhiman