【问题标题】:TypeError: 'str' object is not callable when making a peewee modelTypeError: 'str' 对象在制作 peewee 模型时不可调用
【发布时间】:2019-01-01 21:41:37
【问题描述】:

我目前正在做我自己的小项目,该项目是关于单击一个无意义的按钮。我正在使用 tkinter 和 peewee 在 python 中制作它,以便能够存储保存,以便用户/玩家可以从他们离开的地方继续。

当我制作“保存”功能时,但是当我在 peewee 中创建模型时,它给了我一个错误:

TypeError: 'str' 对象不可调用

我的模型类如下所示:

db = SqliteDatabase("scores.db")


class Score(Model):
    save = CharField()
    score = IntegerField()

    class Meta:
        database = db

保存进度的函数如下所示:

def save_progress():
    global score_number
    # score_number signifies the score of the game(how many times the button has been clicked)
    saves_length = int(Score.select().count())
    save = "save{}".format(saves_length+1)
    Score.create(save=save, score=score_number)

错误来了:

Score.create(save=save, score=score_number)

我不明白为什么它说我正在调用一个字符串对象,因为我认为我不是。

有人可以向我解释一下我在代码中做错了什么吗?

谢谢!

安德烈

【问题讨论】:

    标签: python python-3.x peewee


    【解决方案1】:

    Score 类有一个名为 save 的函数,您将其更改为 str。稍后它会被调用以将行存储到数据库中。

    class Score(Model):
        save = CharField()
    

    查看文档。

    http://docs.peewee-orm.com/en/latest/peewee/models.html

    字段命名冲突

    模型类实现了许多类和实例方法,例如 Model.save() 或 Model.create()。如果您声明一个名称与模型方法一致的字段,则可能会导致问题。考虑:

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 2019-03-05
      • 1970-01-01
      • 2013-05-05
      • 2020-09-16
      • 2019-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多