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

I’m writing a ToDo list app to help myself get started with Python. The app is running on GAE and I’m storing todo items in the Data Store. I want to display everyone’s items to them, and them alone. The problem is that the app currently displays all items to all users, so I can see what you write, and you see what I write. I thought casting my object to a string and seeing if it matches the user’s name would be a good start, but I can’t figure out how to do that.

This is what I have in my

user = users.get_current_user()

if user:
    nickname = user.nickname()
    todos = Todo.all()
    template_values = {'nickname':nickname, 'todos':todos}

def post(self):

    todo = Todo() = users.get_current_user()
    todo.item = self.request.get("item")
    todo.completed = False


In my index.html I had this originally:

<input type="text" name="item" class="form-prop" placeholder="What needs to be done?" required/>
{% for todo in todos %}
  <input type="checkbox"> {{todo.item}} <hr />
{% endfor %}

but I’d like to display items only to the user who created them. I thought of trying

{% for todo in todos %}
    {% ifequal nickname %}
  <input type="checkbox"> {{todo.item}} <hr />
    {% endifequal %}
{% endfor %}

to no avail. The list turns up blank. I assumed it is because is not a string. Can I read the value out as a string, or can I cast the object to String?


Edit: Here is my Todo class

class Todo(db.Model):
author = db.UserProperty()
item = db.StringProperty()
completed = db.BooleanProperty()
date = db.DateTimeProperty(auto_now_add=True)

Will changing my author to a StringProperty effect anything negatively? Maybe I can forgo casting altogether.

5 Answers

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 in your template.

In Python we can use the __str__() method.

We can override it in our class like this:

class User: 

    firstName = ''
    lastName = ''

    def __str__(self):
        return self.firstName + " " + self.lastName

and when running


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()) 


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.

You should define the __unicode__ method on your model, and the template will call it automatically when you reference the instance.

In function post(): = users.get_current_user()

So, to get str(, you need str(users.get_current_user()). What is returned by get_current_user() function ?

If it is an object, check does it contain a str()” function?

I think the error lies there.

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Privacy Policy