How to make the argument to filter() a variable?
I have this model
class Item(db.Model):
...
glam = db.StringProperty()
casual = db.StringProperty()
speaking = db.StringProperty()
and this handler with开发者_如何学C a form with radio buttons:
class SortForm(webapp.RequestHandler):
def get(self):
self.response.out.write("""
<form name="submit_form" action="/sortformhandler" method="post">
Category: <input type="radio" name="image" value="image"> Image <br />
Sorty by tag: <br />
<input type="radio" name="tag" value="glam" /> Glam <br />
<input type="radio" name="tag" value="formal" /> Formal <br />
<input type="radio" name="tag" value="speaking" /> Speaking <br />
<input type="submit" value="submit">
</form>
""")
and this handler
class SortFormHandler(webapp.RequestHandler):
def post(self):
query = Item.all()
query.filter("glam =", "glam")
for item in query:
self.response.out.write("""<a href="%s"><image src="%s" height="110">%s</a>""" %
(item.url, item.image_source_url, item.title) )
I've been trying to have something like
query.filter("[self.request.get("tag")] =", [self.request.get("tag")])
so that when glam
is chosen in the radio button I should have
query.filter("glam =", "glam")
But I could not make this work. In other words, I am trying to make the argument to filter()
a variable. Any suggestions?
I am trying to create tagging for the image library. Thanks.
Would that do what you're looking for:
choice = self.request.get("tag")
query.filter(choice, choice)
However, I agree with Wooble below. The way you have designed it, you dont' really use glam
, casual
, speaking
as StringProperty
, since they are either empty or have a specific value.
What you probably want to do is have a tag
property that can take different values from glam, formal, speaking, ...
class Item(db.Model):
...
tag = db.StringProperty()
And then you would query your db like so:
query.filter("tag", self.request.get("tag"))
query.filter(self.request.get("tag"), self.request.get("tag"))
The =
is actually not required.
However, I'd also consider using a single StringProperty for the tag, since it appears that your 3 string properties are essentially booleans, and only one of them can be true at a time.
精彩评论