按日期部分过滤
我的模型
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()
我需要在上个月之前得到行。我使用交互式控制台。何时这样进行
q = News.all().filter('created_at.month = ',datetime(2011,04,01).month)
print q[0].title
过滤
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Google App Engine 小组中得到了类似您的解决方案。
I was get something like your solution in the Google App Engine group.
要基于月份进行查询,我建议向您的模型添加一个额外的派生字段,该字段会在保存对象时自动更新:
编辑:重新阅读您的问题,看起来您是并不是真正查找给定月份(例如任何一年的十月)的所有帖子,而是仅查找单个月份的帖子。如果是这种情况,那么您应该能够将查询修改为简单的比较:
编辑#2:正如下面 Nick 所指出的,重写
put()
是一种不好的形式,因为它只有在作为绑定方法,并且不适用于 db.put() 或其他保存到数据存储的方法。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:
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:
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 withdb.put()
or other ways of saving to the datastore.