按日期部分过滤

发布于 2024-10-29 18:49:32 字数 438 浏览 3 评论 0原文

我的模型

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2024-11-05 18:49:32

我在 Google App Engine 小组中得到了类似您的解决方案。

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 

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 
一城柳絮吹成雪 2024-11-05 18:49:32

要基于月份进行查询,我建议向您的模型添加一个额外的派生字段,该字段会在保存对象时自动更新:

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)

编辑:重新阅读您的问题,看起来您是并不是真正查找给定月份(例如任何一年的十月)的所有帖子,而是仅查找单个月份的帖子。如果是这种情况,那么您应该能够将查询修改为简单的比较:

# 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))

编辑#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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文