为什么我的 GQL 查询在我的 GAE 应用程序中没有返回任何结果?

发布于 2024-10-01 08:38:52 字数 1141 浏览 4 评论 0原文

我主要遵循了有关如何使用 GAE 和 Python 构建留言簿的教程。现在我只想显示特定日期的条目,但 GQL 查询不会返回任何内容(尽管有当天的条目):

class Shout(db.Model):  
    message= db.StringProperty(required=True)  
    when = db.DateTimeProperty(auto_now_add=True)  
    who = db.StringProperty()  

class MainHandler(webapp.RequestHandler):  
    def get(self):  
        shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')")  
        # Return something without the WHERE clause
        values = {'shouts':shouts}  
        self.response.out.write(template.render('main.html',values))  

    def post(self):  
        self.response.out.write("posted")  
        shout = Shout(message=self.request.get("message"),who=self.request.get("who"))  
        shout.put()  

这是我的 main.html:

<form method="post">
   <input type="text" name="who"></input>
   <input type="text" name="message"></input>
   <input type="submit" value="Send"> </input>  
</form>

{% for shout in shouts  %}
   <div>{{shout.message}} from {{shout.who}} on {{shout.when}}</div>
{% endfor %}

I have mostly followed a tutorial about how to build a gustbook using GAE and Python. Now I only want to show the entries from a particular day, but the GQL query doesn't return anything (although there are entries from that day):

class Shout(db.Model):  
    message= db.StringProperty(required=True)  
    when = db.DateTimeProperty(auto_now_add=True)  
    who = db.StringProperty()  

class MainHandler(webapp.RequestHandler):  
    def get(self):  
        shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')")  
        # Return something without the WHERE clause
        values = {'shouts':shouts}  
        self.response.out.write(template.render('main.html',values))  

    def post(self):  
        self.response.out.write("posted")  
        shout = Shout(message=self.request.get("message"),who=self.request.get("who"))  
        shout.put()  

This is my main.html:

<form method="post">
   <input type="text" name="who"></input>
   <input type="text" name="message"></input>
   <input type="submit" value="Send"> </input>  
</form>

{% for shout in shouts  %}
   <div>{{shout.message}} from {{shout.who}} on {{shout.when}}</div>
{% endfor %}

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

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

发布评论

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

评论(2

零度° 2024-10-08 08:38:52

可能还有另一种方法可以解决这个问题,但我认为因为您的 when 属性是一个日期时间,所以您最好使用这样的方法:

shouts = db.GqlQuery("""SELECT * 
                          FROM Shout 
                         WHERE when >= DATE('2010-11-05')
                           AND when < DATE('2010-11-06')""")

There might be another way around this, but I think that because your when property is a datetime you would be better served with something like this:

shouts = db.GqlQuery("""SELECT * 
                          FROM Shout 
                         WHERE when >= DATE('2010-11-05')
                           AND when < DATE('2010-11-06')""")
您的好友蓝忘机已上羡 2024-10-08 08:38:52

尝试一下:

shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')").fetch(5000)

虽然可以将 Query 对象用作可迭代对象,但最好显式获取行,而不是依赖模板中的 for 来完成工作。我怀疑它不以这种方式支持。

编辑:
现在我更仔细地观察这一点,我怀疑问题在于您正在查询的字段是 DateTimeProperty,并且通过使用 DATE 运算符,您实质上是在说您想要 2010-11-05 00:00:00 ,并且没有包含该确切日期和时间的记录,因此请尝试以下操作:

shouts = db.GqlQuery("SELECT * FROM Shout WHERE when >= DATETIME('2010-11-05 00:00:00') and when <= DATETIME('2010-11-05 23:59:59')")

Try this:

shouts = db.GqlQuery("SELECT * FROM Shout WHERE when=DATE('2010-11-05')").fetch(5000)

While it is possible to use a Query object as an iterable, it is a better idea to explicitly fetch the rows and not depend on the for in your template to do the work. I suspect that it is not supported in that way.

EDIT:
Now that I look more closely at this, I suspect that the problem is that the field you are querying on is a DateTimeProperty, and by using the DATE operator, you are essentially saying that you want 2010-11-05 00:00:00, and there are no records with that exact date and time, so try this instead:

shouts = db.GqlQuery("SELECT * FROM Shout WHERE when >= DATETIME('2010-11-05 00:00:00') and when <= DATETIME('2010-11-05 23:59:59')")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文