Search code examples
pythonpython-3.xpeewee

TypeError: 'str' object is not callable when making a peewee model


I am currently working on my own little project which is about clicking a pointless button. I am making it in python using tkinter and peewee to be able to store saves so that the user/player can continue from where they left.

when I was making a 'save' function, but when I was creating a model in peewee it gave me an error:

TypeError: 'str' object is not callable

My Model class looks like this:

db = SqliteDatabase("scores.db")


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

    class Meta:
        database = db

and the function that saves the progress looks like this:

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)

The error comes on the line:

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

I don't understand why it says that i'm calling a string object because I don't think I am.

Can someone please explain to me what I did wrong in my code?

Thanks!

Andrei


Solution

  • The Score class has a function called save and you changed that to a str. Later it gets called to store the row into the DB.

    class Score(Model):
        save = CharField()
    

    See the docs.

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

    Field-naming conflicts

    Model classes implement a number of class- and instance-methods, for example Model.save() or Model.create(). If you declare a field whose name coincides with a model method, it could cause problems. Consider: