[英]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. 嗨,我正在使用Flask Peewee并尝试更新merchant_details模型,但它无法正常工作。 Following is the error I am getting:
以下是我得到的错误:
AttributeError: 'SelectQuery' object has no attribute 'update'
AttributeError:'SelectQuery'对象没有属性'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! 请帮忙!
First, it looks like you are using pre-2.0 syntax (the filter
method is now deprecated). 首先,看起来您使用的是2.0之前的语法(现在不推荐使用
filter
方法)。 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. 1.)使用查询检索对象,然后使用
save()
方法更新对象。 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... 2.)使用SQL更新语句...
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).
第一个将对数据库进行两次查询(一个用于SELECT记录,另一个用于UPDATE记录),而第二个只用一个SQL调用(用于更新记录)。
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. 我也搜索了这个解决方案,感谢@Mark和@Rohit我改变了我的代码(peeweee与PostgreSQL)并且正在工作。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.