Reputation: 31
My model
class BaseArticleModel(polymodel.PolyModel):
title = db.StringProperty()
created_at = db.DateProperty(auto_now_add=True)
updated_at = db.DateProperty(auto_now=True)
class News(BaseArticleModel):
body = db.TextProperty()
I need get rows by last month. I use interactive console. When to do filter like this
q = News.all().filter('created_at.month = ',datetime(2011,04,01).month)
print q[0].title
I get "IndexError: The query returned fewer than 1 results"
Upvotes: 0
Views: 1426
Reputation: 6794
To query based on a month, I'd suggest adding an additional derived field to your model which is automatically updated when the object gets saved:
class BaseArticleModel(polymodel.PolyModel):
title = db.StringProperty()
created_at = db.DateProperty(auto_now_add=True)
updated_at = db.DateProperty(auto_now=True)
# will be auto-generated
created_at_month = db.IntegerProperty()
def put(self, *args, **kwargs):
self.created_at_month = self.created_at.month
super(BaseArticleModel, self).put(*args, **kwargs)
Edit: Re-reading your question, it looks like you're not really looking for all posts from a given month (e.g. October of any year), but rather just posts in a single month. If that's the case then you should be able to just revise your query to a simple comparison:
# get News posted in April, 2011
all_news = News.all()
all_news.filter('created_at >=', datetime(2011, 4, 1))
all_news.filter('created_at <', datetime(2011, 5, 1))
Edit #2: As noted by Nick below, overriding put()
would be bad form, since it would only be effective when called as a bound method, and will NOT work with db.put()
or other ways of saving to the datastore.
Upvotes: 1
Reputation: 31
I was get something like your solution in the Google App Engine group.
class BaseArticleModel(polymodel.PolyModel):
title = db.StringProperty()
created_at = db.DateProperty(auto_now_add=True)
updated_at = db.DateProperty(auto_now=True)
@db.ComputedProperty
def CM(self):
return self.created_at.month
Upvotes: 3