TA4 – Searching
So I actually made the searching pretty easy on myself, and looked at it like a filter. The advanced search dropdowns had a bunch of default value that, if they didn’t change, simply returned the entire set. So if the user did not specify any criteria for a TA search, all the TAs would simply be returned. Since I took this approach, a class search and TA search had to be different; one would return only TAs, and one would return only classes.
Google Appengine makes it actually really really easy to do something like this. Our TA model is named “Ta” and the instances of each course is “Section”. I imported the models into my page, and if it was a TA search I created a query object with “Ta.all()” which returned a list of all the Ta objects. For classes it was the same, with Section.all().
Like I said, this returned a Query object (documented at http://code.google.com/appengine/docs/python/datastore/queryclass.html) on which I could use to filter out different results, if the person had actually selected something. For example, if the person had all the defaults selected and then picked “Native English” to be yes, the code looked like:
1.
query = Ta.all()
2.
if self.request.get(‘nativeEnglish’):
3.
query.filter(“nativeEnglish =”, self.request.get(‘nativeEnglish’))
which will filter out the TAs that speak English natively.
To match on reference properties is slightly more difficult than that. For instance, if I want to filter out TAs that have a specific major, what is actually saved in the major property of the TA table is not a string “Computer Science” but rather a Major object with the name “Computer Science”. So you can’t just do “query.filter(”major=”,”Computer Science”). You have to query a major object from the database like so:
1.
if self.request.get(‘major’):
2.
majorInstance = db.GqlQuery(“SELECT * FROM Major WHERE name = :1″, self.request.get(‘major’)).get()
3.
query.filter(“major =”, majorInstance)
The text search was pretty painless, because Google has something that makes it super easy. If I type something in and I want to see if it matches any of the TAs, I just use:
query = Ta.all().search(“Phillip”)
One other note. Since the query object itself is an iterator, I don’t have to call the filter() method on it in order to iterate over the list. I simply filter down through the search options and pass the Query object to my template and iterate over that.
References:
http://daily.profeth.de/2008/04/er-modeling-with-google-app-engine.html
http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#ReferenceProperty
http://code.google.com/appengine/docs/python/datastore/queryclass.html#Query_filter
http://code.google.com/appengine/docs/python/datastore/creatinggettinganddeletingdata.html#Getting_an_Entity_Using_a_Key
Comments
Leave a comment Trackback