从Google App Engine数据存储中列出所有实体时出现AttributeError

3 投票
1 回答
1265 浏览
提问于 2025-04-18 03:22

我正在尝试从我的数据存储中获取所有的实体,然后用HTML显示它们。我是在一个 RequestHandler 里面做这个,但我遇到了一个错误信息。

"AttributeError: type object 'Student' has no attribute 'all'"

这是我的 Student 类的代码:

class Student(ndb.Model):
   banner_id = ndb.IntegerProperty(required=True)
   name=ndb.StringProperty()
   score=ndb.IntegerProperty()

这是 RequestHandler 的代码:

class MainHandler(webapp2.RequestHandler):
def get(self):
    # Create a HTML table
    table = "<html><head><title>Students Server</title></head><body><table><th>name</th><td>score</th>"
    # Now get a list of all students
    sqry = Student.all()
    sqry.order('name')

        # Use the data collected so far to create a table row and add 
        # it to the table
    table += Student.toTableRow(score)
    # Complete the table
    table += "</table>"
    self.response.write(table)
    self.response.write(studentRegistrationPage)

我想获取所有学生,并根据名字对列表进行排序。这个想法是从 这里 得到的,那里有类似的例子。

# Order alphabetically by last name:
q = Person.all()
q.order('last_name')

# Order by height, tallest to shortest:
q = Person.all()
q.order('-height')

我哪里做错了呢?

1 个回答

2

你正在尝试进行一个 db 查询,但你使用的是 ndb

可以看看这份文档 https://developers.google.com/appengine/docs/python/ndb/queries。还可以参考一下 db/ndb 的速查表 https://docs.google.com/document/d/1AefylbadN456_Z7BZOpZEXDq8cR8LYu7QgI7bt5V0Iw/mobilebasic?pli=1

使用 ndb 的话,查询应该写成 Person.query()

撰写回答