Does Python have a toString() equivalent, and can I convert a db.Model element to String? Does Python have a toString() equivalent, and can I convert a db.Model element to String? python python

Does Python have a toString() equivalent, and can I convert a db.Model element to String?


In python, the str() method is similar to the toString() method in other languages. It is called passing the object to convert to a string as a parameter. Internally it calls the __str__() method of the parameter object to get its string representation.

In this case, however, you are comparing a UserProperty author from the database, which is of type users.User with the nickname string. You will want to compare the nickname property of the author instead with todo.author.nickname in your template.


In Python we can use the __str__() method.

We can override it in our class like this:

class User:     def __init__(self):        self.firstName = ''        self.lastName = ''        ...            def __str__(self):        return self.firstName + " " + self.lastName

and when running

print(user)

it will call the function __str__(self) and print the firstName and lastName


str() is the equivalent.

However you should be filtering your query. At the moment your query is all() Todo's.

todos = Todo.all().filter('author = ', users.get_current_user().nickname()) 

or

todos = Todo.all().filter('author = ', users.get_current_user())

depending on what you are defining author as in the Todo model. A StringProperty or UserProperty.

Note nickname is a method. You are passing the method and not the result in template values.