How to use Update query in Flask Peewee?
Asked Answered
U

3

7

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!

Uraeus answered 8/10, 2013 at 4:50 Comment(0)
P
21

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

Privy answered 8/10, 2013 at 5:52 Comment(2)
MerchantDetails.update(MerchantDetails.name='new name') this is an invalid Python syntax.Hackneyed
I fixed it, it was a small syntax errorGagger
U
3

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

Uraeus answered 8/10, 2013 at 5:54 Comment(0)
H
0

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()
Hothouse answered 9/2, 2019 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.