views:

821

answers:

3

Ok, you guys were quick and helpful last time so I'm going back to the well ;)

Disclaimer: I'm new to python and very new to App Engine. What I'm trying to do is a simple modification of the example from the AppEngine tutorial.

I've got my date value being stored in my Memory class:

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

and now I want to be able to lookup records for a certain date. I wasn't sure exactly how to do it so I tried a few things including:

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

But every time I run it, I get an ImportError. Frankly, I'm not even sure how to parse these error message I get when the app fails but I'd just be happy to be able to look up the Memory records for a certain date.

EDIT: FULL SOURCE BELOW

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()
+1  A: 
memories.filter("date=DATE(2007, 7, 20)")

Refer to GQL Reference.

an0
I tried that and I'm still getting the error. If I take out the date filter line, everything runs fine.
Shane
But thanks for the link to the GQL reference. I was looking for something related to how to compare date types but was unsuccessful. I guess I didn't try too hard ;) I'll mess around with it in the meantime.
Shane
A: 
class Memory(db.Model):
    author = db.UserProperty()
    content = db.StringProperty(multiline=True)
    date = db.DateProperty(auto_now_add=True)

I think that you are either getting import errors for memory or your are getting an import error for datetime

if Memory is in another .py file, e.g otherpyfile.py, you will need to do from otherpyfile import Memory and then use it that way

if its a datetime issue then you will need to import datetime. Your first answer had a mismatch of quotes so sorted that. I sorted your middle one so that if you 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'))

The appengine error screen isn't always helpful so have a look at the logs that are being thrown in the command prompt. If you see that error it might be worth dumping the short stacktrace it does so I can try help you further.

AutomatedTester
Thanks, this got me a bit closer (I think!) I'm not sure if I had the quote error when running before but now, I don't get any error with a hardcoded date = '2007-07-20', however I don't get any results even though I know there are entries with that date posted to the datastore.I've amended the question with the entirety of the source code. Maybe I'm missing something very simple.
Shane
Ok, in the end, the 2nd version worked out for me finally. I thought I had tried it exactly like that but must have been missing something.Thanks for the replies, everyone.
Shane
A: 

You're trying to use GQL syntax with non-GQL Query objects. Your options are:

  1. Use the Query object and pass in a datetime.date object: q = Memory.all().filter("date =", datetime.date.today())
  2. Use a GqlQuery and use the DATE syntax: q = db.GqlQuery("SELECT * FROM Memory WHERE date = DATE(2007, 07, 20)")
  3. Use a GqlQuery and pass in a datetime.date object: q = db.GqlQuery("SELECT * FROM Memory WHERE date = :1", datetime.date.today())
Nick Johnson
Were you able to run this code yourself? Maybe I have something else wrong aside from just this one call? Which seems unlikely but I'm running out of ideas :(
Shane
Er, sorry, I didn't see that the error you're getting is an ImportError. That has nothing to do with GQL or queries - and you need to show us the stacktrace for the error if we're to help at all.
Nick Johnson