GQL SELECT by date
I have this model:
class Entry(db.Model):
title = db.StringProperty()
url = db.URLProperty()
date = db.DateTimeProperty(auto_now=True)
image = db.URLProperty()
weight = db.IntegerProperty()
category = db.StringProperty()
desc = db.TextProperty()
I have many posts every day, how can I select today's posts only with GGL? since a query like this returns no results (but I know there are results):
SELECT * FROM Entry WHERE category = 'news' and date = '2012-03-12' ORDER BY weight DESC
source to share
It is not stored as a string, but as a datetime object.
The right side of the comparison can be one of the following:
datetime, date or time literal with numeric values or string representation in the following forms:
DATETIME (year, month, day, hour, minute, second)
DATETIME ("YYYY-MM-DD HH: MM: SS")
DATE (year, month, day)
DATE ('YYYY-MM-DD')
TIME (hour , minute, second)
TIME ('HH: MM: SS')
http://code.google.com/appengine/docs/python/datastore/gqlreference.html
So, in your example, use either one.
date = DATE('2012-03-12')
date = DATE(2012,03,12)
During datetime, the time is set to 00:00 by default, so no equality comparison will be done, so you must use> to compare
SELECT * FROM Entry WHERE date > DATETIME(yyyy,mm,dd)