Manas Chaturvedi
Manas Chaturvedi

Reputation: 5540

Google App Engine: Database object is not iterable

I'm working on an ecommerce site, using Google App Engine with Python. Here's the entity 'Products' in my datastore:

enter image description here

Now, I'm trying to filter out certain products based on the user preference. The user has the option of selecting these preferences with the help of drop-down options. Here's the code for the same in '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>   

Here's my python code:

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)  

However, after the user selects and submits his/her's preferred filtering conditions, I get the following error:

TypeError: 'Products' object is not iterable

What seems to be the problem with my code? Why are the entries in my database not iterable?

Upvotes: 1

Views: 1955

Answers (2)

Gwyn Howell
Gwyn Howell

Reputation: 5424

Use fetch() to return a list of entities. get() just returns the first entity that matches your query, therefore, it is not iterable.

Upvotes: 4

jterrace
jterrace

Reputation: 67073

You're calling get(), which only "returns the first result". You should instead call fetch(), which returns a list of results.

Upvotes: 2

Related Questions