python - Peewee Model 关键字不能是更新上的表达式

标签 python orm model sql-update peewee

正如您从下面看到的,我正在尝试更新记录,但由于某种原因我收到了这些错误。我一直在关注 Peewee 文档,我发现其他一些人也有同样的问题,但不是 Djano 的 ORM,不是 Peewee。我尝试输入“__”而不是“.”。分类器没有任何运气。

经过测试的代码

Python 2.7.9 (default, Feb  1 2015, 21:31:28) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Wee_Models import Thunderdome, ThunderdomeLog
>>> from peewee import *
>>> import peewee
>>> db = MySQLDatabase(database="GrabaDB", host="localhost", port=3306, user="demo", passwd="demo")
>>> q = Thunderdome.update(status='Updated status').where(Thunderdome.port='2310')
  File "<stdin>", line 1
SyntaxError: keyword can't be an expression
>>> q = Thunderdome.update(status='Updated status').where(Thunderdome__port='2310')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 272, in inner
    func(clone, *args, **kwargs)
TypeError: where() got an unexpected keyword argument 'Thunderdome__port'
>>> 

雷霆穹顶模型:

class Thunderdome(BaseModel):
    date = DateTimeField()
    lock_status = IntegerField()
    port = PrimaryKeyField()
    security_key = TextField(null=True)
    serial = TextField(null=True)
    status = TextField(null=True)

    class Meta:
        db_table = 'ThunderDome'

我正在关注 Peewee 的文档:

http://peewee.readthedocs.org/en/latest/peewee/api.html#Model.update

http://peewee.readthedocs.org/en/latest/peewee/querying.html

Updating existing records
Once a model instance has a primary key, any subsequent call to save() will result in an UPDATE rather than another INSERT. The model’s primary key will not change:

>>> user.save()  # save() returns the number of rows modified.
1
>>> user.id
1
>>> user.save()
>>> user.id
1
>>> huey.save()
1
>>> huey.id
2
If you want to update multiple records, issue an UPDATE query. The following example will update all Tweet objects, marking them as published, if they were created before today. Model.update() accepts keyword arguments where the keys correspond to the model’s field names:

>>> today = datetime.today()
>>> query = Tweet.update(is_published=True).where(Tweet.creation_date < today)
>>> query.execute()  # Returns the number of rows that were updated.
4
For more information, see the documentation on Model.update() and UpdateQuery.

最佳答案

你必须写:

q = Thunderdome.update(status='Updatedstatus')\
     .where(Thunderdome.port == '2310')

请参阅文档 http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=update#Model.update

关于python - Peewee Model 关键字不能是更新上的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814003/

相关文章:

python - 3 个 cython/python 函数调用之间的差异

通过 Macports 安装 Python 3.3 后,Python 版本消失了

python - 将数组中的元素转换为python中的 float

java - 如何解决由于继承类而导致的org.hibernate.MappingException

django - 在 DB 中保存 Django 的表单向导 form_list 的最简单方法?

python - 如何在优雅地保存在 Django 1.5 之前使用 full_clean() 进行数据验证?

python - Django 模型 OneToOneField : "This field is required" error in admin

python - Conda 环境中的 LD_LIBRARY_PATH

java - java 'main' 应用程序中的工作单元

sql - Rails - 如何按 ID 范围选择所有记录