【问题标题】:Google App Engine: Database object is not iterableGoogle App Engine:数据库对象不可迭代
【发布时间】:2014-04-03 16:42:40
【问题描述】:

我正在开发一个电子商务网站,使用带有 Python 的 Google App Engine。这是我的数据存储中的实体“产品”:

现在,我正在尝试根据用户偏好过滤掉某些产品。用户可以在下拉选项的帮助下选择这些首选项。这是“newbooks.html”中相同的代码:

<form method = "post">
        <p>Department: </p>
        <select name = "branch">
            <option></option>
            <option>Computers</option>
            <option>EXTC</option>
            <option>IT</option>
            <option>ETRX</option>
            <option>MECH</option>
        </select>
        <br>
        <br>
        <p>Semester: </p>

        <select name = "semester">
            <option></option>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
        </select>

        <br>
        <br>
        <p>Publications: </p>
        <select name = "publications">
            <option></option>
            <option>Techmax</option>
            <option>Nandu</option>
            <option>Pearson</option>
            <option>Tata McGraw Hill</option>
            <option>Technical</option>
        </select>
            <br>
        <br>
        <p>Subject: </p>
        <select name = "subject">
            <option></option>
            <option>System Programming and Compiler Construction</option>
            <option>Advanced Microprocessors</option>
            <option>Microprocessors and Interfacing</option>
            <option>Computer Networks</option>
            <option>Data Warehouse and Data Mining</option>
        </select>    

        <br><br>

        <input type = "submit">   
        </form>   

这是我的python代码:

class NewBooks(Handler):
    def get(self):
        self.render("newbooks.html")
    def post(self):
        branch = self.request.get("branch")
        semester = self.request.get("semester")
        publications = self.request.get("publications")
        subject = self.request.get("subject")
        if semester:
            yo = int(semester)
        if(branch and semester and publications and subject):
            disp = Products.all().filter("branch =", branch).filter("publisher =", publications).filter("name =", subject).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)
        elif(branch and semester and publications):
            disp = Products.all().filter("branch =", branch).filter("publisher =", publications).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp) 
        elif(branch and semester and subject):
            disp = Products.all().filter("branch =", branch).filter("name =", subject).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)  
        elif(branch and publications and subject):
            disp = Products.all().filter("branch =", branch).filter("publisher =", publications).filter("name =", subject).get()
            self.render("newbooks.html", disp = disp)
        elif(semester and publications and subject):
            disp = Products.all().filter("publisher =", publications).filter("name =", subject).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)
        elif(branch and semester):
            disp = Products.all().filter("branch =", branch).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)
        elif(semester and publications):
            disp = Products.all().filter("publisher =", publications).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)  
        elif(publications and subject):
            disp = Products.all().filter("publisher =", publications).filter("name =", subject).get()
            self.render("newbooks.html", disp = disp)
        elif(branch and subject):
            disp = Products.all().filter("branch =", branch).filter("name =", subject).get()
            self.render("newbooks.html", disp = disp) 
        elif(branch and publications):
            disp = Products.all().filter("branch =", branch).filter("publisher =", publications).get()
            self.render("newbooks.html", disp = disp)
        elif(semester and subject):
            disp = Products.all().filter("name =", subject).filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp) 
        elif(branch):
            disp = Products.all().filter("branch =", branch).get()
            self.render("newbooks.html", disp = disp) 
        elif(semester):
            disp = Products.all().filter("semester =", yo).get()
            self.render("newbooks.html", disp = disp)
        elif(publications):
            disp = Products.all().filter("publisher =", publications).get()
            self.render("newbooks.html", disp = disp)  
        elif(subject):
            disp = Products.all().filter("name =", subject).get()
            self.render("newbooks.html", disp = disp)  

但是,在用户选择并提交他/她的首选过滤条件后,我收到以下错误:

TypeError: 'Products' object is not iterable

我的代码似乎有什么问题?为什么我的数据库中的条目不可迭代?

【问题讨论】:

  • 不要使用 get()。这给出了一个条目。跳过它或使用 fetch()

标签: python google-app-engine


【解决方案1】:

使用 fetch() 返回实体列表。 get() 只返回与您的查询匹配的第一个实体,因此它是不可迭代的。

【讨论】:

  • 这是我在代码中用 fetch() 替换 get() 时遇到的错误:“TypeError: fetch() 至少需要 2 个参数(1 个给定)”
  • 您需要提供一个限制参数。例如 fetch(100) 将返回前 100 个实体
【解决方案2】:

您正在调用get(),它只会“返回第一个结果”。您应该改为调用 fetch(),它会返回结果列表。

【讨论】:

    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2011-06-17
    • 2011-06-17
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多