【问题标题】:AttributeError when listing all entities from Google App Engine datastore列出来自 Google App Engine 数据存储区的所有实体时出现 AttributeError
【发布时间】:2014-04-19 07:13:16
【问题描述】:

我正在尝试获取我的数据存储中的所有实体,然后使用 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)

我正在尝试检索所有学生并根据姓名对列表进行排序。从here 得到这个想法,那里给出了一个这样的例子。

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

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

我做错了什么?

【问题讨论】:

    标签: python google-app-engine app-engine-ndb


    【解决方案1】:

    您正在尝试执行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()

    【讨论】:

    • 蒂姆来救援,再次。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多