如何使 filter() 的参数成为变量?

发布于 2024-10-14 18:41:39 字数 1639 浏览 3 评论 0原文

我有这个模型

class Item(db.Model):
    ...   
    glam = db.StringProperty()
    casual = db.StringProperty()
    speaking = db.StringProperty()

和这个带有单选按钮表单的处理程序:

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>
            """)

并且这个处理程序

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)  )

我一直在尝试拥有类似的东西

        query.filter("[self.request.get("tag")] =", [self.request.get("tag")])

,以便当在单选按钮中选择 glam 时我应该有

        query.filter("glam =", "glam")

但我不能使这项工作发挥作用。换句话说,我试图将 filter() 的参数设为变量。有什么建议吗?

我正在尝试为图像库创建标签。谢谢。

I have this model

class Item(db.Model):
    ...   
    glam = db.StringProperty()
    casual = db.StringProperty()
    speaking = db.StringProperty()

and this handler with 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滿滿的愛 2024-10-21 18:41:39

这会满足您的需求吗:

choice = self.request.get("tag")
query.filter(choice, choice)

但是,我同意下面的 Wooble 观点。按照您的设计方式,您实际上并没有使用 glamcasualspeaking 作为 StringProperty,因为它们要么为空,要么具有特定值。

您可能想要做的是拥有一个 tag 属性,该属性可以采用来自 glamformalspeaking 的不同值,...

class Item(db.Model):
    ...   
    tag = db.StringProperty()

然后你会像这样查询你的数据库:

query.filter("tag", self.request.get("tag"))

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"))
む无字情书 2024-10-21 18:41:39
query.filter(self.request.get("tag"), self.request.get("tag"))

= 实际上不是必需的。

不过,我还会考虑为标记使用单个 StringProperty,因为看起来您的 3 个字符串属性本质上是布尔值,并且一次只能其中一个为 true。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文