I'm trying to write a fetch query for DataStore.
In datastore it has date field column with this format 2018-02-20 (11:07:36.000) MMT
I want to fetch only data of the specified date
I'm doing the following but it's not working:
def query(self, date=None):
query = self.client.query(kind='Test')
if date != None:
query.add_filter('date', '>', datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d'))
query.add_filter('date', '<', datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d'))
results = list(query.fetch())
print(results)
return results
if __name__ == '__main__':
date = 20181012
data_list = query(date)
What am I doing wrong?
Side note: your date
is an integer, passing it to datetime.strptime(date, '%Y%m%d')
won't work, you'll get a TypeError: strptime() argument 1 must be string, not int
. You'd need to use a string, not an int. But that's not really the problem.
In datastore it has date field column with this format 2018-02-20 (11:07:36.000) MMT
The format is irrelevant, you shouldn't operate with timestamp properties as strings. In other words you shouldn't needs to use strftime/strptime
. You should deal with them as datetime
objects instead. You can find an example in Restrictions on queries:
start_date = datetime.datetime(1990, 1, 1) end_date = datetime.datetime(2000, 1, 1) query = client.query(kind='Task') query.add_filter( 'created', '>', start_date) query.add_filter( 'created', '<', end_date)
Another important note is that your query with the filters added will never produce any result: no 'date'
property value can simultaneously be both greater than and smaller than the same other value - your datetime.strptime(date, '%Y%m%d').strftime('%Y-%m-%d')
, present in both filters. You need to provide 2 different start and end timestamp values in the 2 filters.
Adapting the above example for your question it'd be something like:
query.add_filter('date', '>=', date)
query.add_filter('date', '<', date + datetime.timedelta(days=1))
and you'd specify 'date'
as a datetime
:
date = datetime.datetime(2018, 10, 12)