Home Ask Login Register

Developers Planet

Your answer is one click away!

abhishek February 2016

Filter by year and month in ndb model

what i am trying to do is that , i am using ndb model as my database and i have a model having DateTimeProperty() property. Now what i want is that i want to fetch all the records which for a particular year and also for a particular month.

class Photo(ndb.Model):
   userid = ndb.StringProperty()

   source = ndb.StringProperty()
   handle = ndb.StringProperty()
   sourceid =ndb.StringProperty()

   viewedCount = ndb.IntegerProperty(default=0)
   likedCount = ndb.IntegerProperty()
   uploadedOn = ndb.DateTimeProperty()
   latitude = ndb.FloatProperty(default=0)
   longitude = ndb.FloatProperty(default=0)

i have a field uploadedOn which has a dateTime property.

queries would be like (i) fetch all records for year 2012 (ii) fetch records for month of January

I have value in format "uploadedOn": "2014-12-27T11:57:01"


Aviah Laor February 2016

import datetime
from_date = datetime.datetime(2015,11,30)
until_date = datetime.datetime(2016,1,1)
    Photo.query(ndb.AND(Photo.uploadedOn>from_date, Photo.uploadedOn<until_date))

doru February 2016

You can do something like:

import datetime

date_wanted = datetime.datetime(2014, 12, 27)
photo = Photo.query().filter(Photo.uploadedOn==date_wanted).fetch()

Post Status

Asked in February 2016
Viewed 2,757 times
Voted 4
Answered 2 times


Leave an answer

Quote of the day: live life