views:

126

answers:

1

I can't seem to select something based on timestamp. The behavior is a bit weird and the < and = symbols don't seem to mean what I expect them to.

""" A site message """
class Message( db.Model ) :
  # from/to/ a few other fields
  subject = db.StringProperty()
  body = db.Text()

  # this is the field i'm trying to use
  sent = db.DateTimeProperty( auto_now_add=True )

When I write GQL queries like

select * from Message where sent = '2009-09-14 01:00:02.648000'

(there is a message with PRECISELY that time stamp in the datastore)

it gives me nothing back.

If I try

select * from Message where sent < '2009-09-14 01:00:02.648000'

It simply gives me all of them. When I try the > sign, it simply gives me none again.

What's going on here and how do I select based on timestamp?

+3  A: 

Do NOT use strings to "stand in" for datetimes! It just won't work...

See the docs: DateTimeProperty corresponds to a datetime.datetime value. import datetime, convert your beloved strings to datetime.datetime instances (e.g. by calling the strptime method thereof), use THOSE instances for comparisons -- and, of course, live happily ever after!-)

Alex Martelli
How do you put an object into a GQL query string without converting it back into a string though?
bobobobo
You can reference datetime like so: SELECT * FROM table WHERE datemodel = DATETIME('YYYY-MM-DD HH:MM:SS') See here: code.google.com/appengine/docs/…
Dominic Bou-Samra
You can also do "SELECT * FROM table WHERE datemodel = :1", and pass the datetime as an argument to gqlquery, which is definitely what you _should_ do if you're using a datetime selected at runtime.
Nick Johnson
@Nick nails it, as usual;-).
Alex Martelli