ndb query error with datetime field - Google App Engine

后端 未结 4 1579
情书的邮戳
情书的邮戳 2020-12-16 17:31

I\'m having a problem and I don\'t find any information about.

I define a field in my model like this.

class Dates(ndb.model):
    ...
    date = ndb         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 18:06

    I expanded @Guido van Rossum code snippet to include <> and timedelta for calculations, mostly for my own satisfaction

    import datetime
    from datetime import timedelta
    
    from google.appengine.ext.ndb import *
    
    class D(Model):
      d = DateProperty()
    
    now = datetime.date.today()
    date1 =  now-timedelta(+500)
    date2 =  now-timedelta(+5)
    
    d1 = D(d=now)
    d2 = D(d=date1)
    d3 = D(d=date2)
    
    d1.put()
    d2.put()
    d3.put()
    
    date2 =  now-timedelta(+50)
    
    result1 = D.query(D.d == now).fetch(4)
    result2 = D.query(D.d > date2).fetch(2)
    result3 = D.query(D.d < date2).fetch(2)
    
    result4 = D.query(D.d >= date2, D.d <= now).fetch(2)
    
    print result1
    print "+++++++"
    print result2
    print "+++++++"
    print result3
    print "+++++++"
    print result4
    

提交回复
热议问题