Python是否具有等效的toString(),并且可以将db.Model元素转换为String吗? [英] Does Python have a toString() equivalent, and can I convert a db.Model element to String?

查看:87
本文介绍了Python是否具有等效的toString(),并且可以将db.Model元素转换为String吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个ToDo列表应用程序,以帮助自己开始使用Python。该应用程序在GAE上运行,我将待办事项存储在数据存储区中。我想向所有人展示每个人的项目,并且只向他们显示。问题在于该应用程序当前向所有用户显示所有项目,因此我可以看到您写的内容,也可以看到我写的内容。我以为将我的todo.author对象转换为字符串,然后查看它是否与用户名匹配是一个很好的开始,但是我不知道该怎么做。

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 todo.author 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.

这就是我的main.py

This is what I have in my main.py

... 
user = users.get_current_user()

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

def post(self):

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

    todo.put()      
    self.redirect('/')

在我的index.html中,最初是这样的:

In my index.html I had this originally:

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

但我只想向创建它们的用户显示项目。我想尝试

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

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

无济于事。列表变成空白。我以为是因为todo.author不是字符串。我可以将值读取为字符串,还是可以将对象转换为String?

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

谢谢!

编辑:这是我的Todo类

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)

将我的作者更改为StringProperty效果会带来负面影响吗?也许我可以完全放弃转换。

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

推荐答案

在python中, str()方法 toString( )方法用其他语言。称为传递对象以将其转换为字符串作为参数。在内部,它调用参数对象的 __ str __()方法以获取其字符串表示形式。

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.

在这种情况下,但是,您正在比较类型为UserProperty 作者python / users / userobjects rel = noreferrer> users.User 和昵称字符串。您将需要比较作者的昵称属性与模板中的 todo.author.nickname

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.

这篇关于Python是否具有等效的toString(),并且可以将db.Model元素转换为String吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆