GAE datastore - count records between one minute ago and two minutes ago?
Question: I am using GAE datastore with python and I want to count and display the
number of records between two recent dates. for examples, how many records
exist with a time signature between two minutes ago and three minutes ago in
the datastore. Thank you.

    
    
    #!/usr/bin/env python
    
    import wsgiref.handlers
    from google.appengine.ext import db
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp import template
    from datetime import datetime
    
    class Voice(db.Model):
        when = db.DateTimeProperty(auto_now_add=True)
    
    
    class MyHandler(webapp.RequestHandler):
        def get(self):
            voices = db.GqlQuery(
                'SELECT * FROM Voice '
                'ORDER BY when DESC')
            values = {
                'voices': voices
            }
            self.response.out.write(template.render('main.html', values))
        def post(self):
            voice = Voice()
            voice.put()
            self.redirect('/')
            self.response.out.write('posted!')  
    
    def main():
        app = webapp.WSGIApplication([
            (r'.*', MyHandler)], debug=True)
        wsgiref.handlers.CGIHandler().run(app)
    
    if __name__ == "__main__":
        main()
    


Answer: 
    count = db.Query().filter('when >', two_minutes_ago).filter('when <', one_minute_ago).count()
    

You can learn more about queries in the
[documentation](https://developers.google.com/appengine/docs/python/datastore/queryclass).

To get the values of `two_minutes_ago` and `one_minute_ago` you can use the
[`datetime`](http://docs.python.org/library/datetime.html) module:

    
    
    >>> datetime.datetime.now()
    datetime.datetime(2012, 4, 14, 14, 26, 18, 343269)
    >>> datetime.datetime.now() - datetime.timedelta(minutes=1)
    datetime.datetime(2012, 4, 14, 14, 25, 49, 860390)
    

Try it out in your Python REPL to get more familiar with it.

