Google App Engine:数据库对象不可迭代

时间:2014-04-03 16:42:40

标签: python google-app-engine

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

enter image description here

现在,我正在尝试根据用户偏好过滤掉某些产品。用户可以选择在下拉选项的帮助下选择这些首选项。这是'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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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