GQL查询Python中的日期相等性

时间:2009-07-22 01:41:32

标签: python google-app-engine

好的,你们上次快速而乐于助人,所以我要回到井里了;)

免责声明:我是python的新手,也是App Engine的新手。我想要做的是从AppEngine教程中对示例进行简单修改。

我的日期值存储在Memory类中:

class Memory(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateProperty(auto_now_add=True)

现在我希望能够查找特定日期的记录。我不知道该怎么做,所以我尝试了一些事情,包括:

memories = db.GqlQuery("SELECT * from Memory where date = '2007-07-20')
and
memories = Memory.all()
memories.filter("date=", datetime.strptime(self.request.get('date'), '%Y-%m-%d').date())
and
memories = Memory.all()
memories.filter("date=", self.request.get('date'))

但每次运行它时,我都会遇到ImportError。坦率地说,我甚至不确定如何解析我在应用程序失败时得到的这些错误消息,但我很高兴能够在某个特定日期查找内存记录。

编辑:完全来源

import cgi
import time

from datetime import datetime
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Memory(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateProperty()

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<html><body>')

        memories = db.GqlQuery('SELECT * from Memory ORDER BY date DESC LIMIT 10')

        for memory in memories:
            self.response.out.write('<b>%s</b> wrote: ' % memory.author.nickname())
            self.response.out.write('<em>%s</em>' % memory.date)
            self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(memory.content))

        self.response.out.write("""
<div style="float: left;">
<form action="/post" method="post">
    <fieldset>
    <legend>Record</legend>
    <div><label>Memory:</label><input type="text" name="content" /></textarea></div>
    <div><label>Date:</label><input type="text" name="date" /></div>
    <div><input type="submit" value="Record memory" /></div>
    </fieldset>
</form>
</div>
<div style="float: left;">
<form action="/lookup" method="post">
    <fieldset>
    <legend>Lookup</legend>
    <div><label>Date:</label><input type="text" name="date" /></div>
    <div><input type="submit" value="Lookup memory" /></div>
    </fieldset>
</form>
</div>""")

        self.response.out.write('</body></html>')

class PostMemory(webapp.RequestHandler):
    def post(self):
        memory = Memory()

        if users.get_current_user():
            memory.author = users.get_current_user()

        memory.content = self.request.get('content')
        memory.date = datetime.strptime(self.request.get('date'), '%Y-%m-%d').date()

        memory.put()
        self.redirect('/')

class LookupMemory(webapp.RequestHandler):
    def post(self):
        memories = db.GqlQuery("SELECT * FROM Memory WHERE date = '2009-07-21'")

        for memory in memories:
            self.response.out.write('<b>%s</b> wrote: ' % memory.author.nickname())
            self.response.out.write('<em>%s</em>' % memory.date)
            self.response.out.write('<blockquote>%s</blockquote>' % cgi.escape(memory.content))     

application = webapp.WSGIApplication([('/', MainPage), ('/post', PostMemory), ('/lookup', LookupMemory)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

3 个答案:

答案 0 :(得分:2)

您正在尝试将GQL语法与非GQL查询对象一起使用。您的选择是:

  1. 使用Query对象并传入datetime.date对象:q = Memory.all().filter("date =", datetime.date.today())
  2. 使用GqlQuery并使用DATE语法:q = db.GqlQuery("SELECT * FROM Memory WHERE date = DATE(2007, 07, 20)")
  3. 使用GqlQuery并传入datetime.date对象:q = db.GqlQuery("SELECT * FROM Memory WHERE date = :1", datetime.date.today())

答案 1 :(得分:1)

memories.filter("date=DATE(2007, 7, 20)")

请参阅GQL Reference

答案 2 :(得分:0)

class Memory(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateProperty(auto_now_add=True)

我认为您要么获取内存的导入错误,要么导致日期时间导入错误

如果Memory在另一个.py文件中,例如otherpyfile.py,则需要执行from otherpyfile import Memory然后以这种方式使用

如果是日期时间问题,那么您需要import datetime。你的第一个答案是如此排序的引号不匹配。我对你的中间人进行了排序,以便你导入日期时间

memories = db.GqlQuery("SELECT * from Memory where date = '2007-07-20'")

memories = Memory.all().filter("date=", datetime.datetime.strptime(self.request.get('date'), '%Y-%m-%d').date())

memories = Memory.all().filter("date=", self.request.get('date'))

appengine错误屏幕并不总是有用,因此请查看命令提示符中引发的日志。如果您看到该错误,可能需要转储短堆栈跟踪,这样做我可以尝试进一步帮助您。