App Engine GQL:查询日期范围

发布于 2024-08-06 02:19:00 字数 622 浏览 7 评论 0原文

此 Django 语句在 App Engine 中的等效项是什么?

return Post.objects.get(created_at__year=bits[0], 
                            created_at__month=bits[1], 
                            created_at__day=bits[2],
                            slug__iexact=bits[3])

我最终写了这个:

Post.gql('WHERE created_at > DATE(:1, :2, :3) AND created_at < DATE(:1, :2, :4) and slug = :5',
    int(bit[0]), int(bit[1]), int(bit[2]), int(bit[2]) + 1, bit[3])

但与 Django 相比,它非常可怕。还有其他更 Pythonic/Django 魔法的方式,例如使用 Post.filter()created_at.day/month/year 属性吗?

What would be the App Engine equivalent of this Django statement?

return Post.objects.get(created_at__year=bits[0], 
                            created_at__month=bits[1], 
                            created_at__day=bits[2],
                            slug__iexact=bits[3])

I've ended up writing this:

Post.gql('WHERE created_at > DATE(:1, :2, :3) AND created_at < DATE(:1, :2, :4) and slug = :5',
    int(bit[0]), int(bit[1]), int(bit[2]), int(bit[2]) + 1, bit[3])

But it's pretty horrific compared to Django. Any other more Pythonic/Django-magic way, e.g. with Post.filter() or created_at.day/month/year attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

浪漫之都 2024-08-13 02:19:00

怎么样

from datetime import datetime, timedelta

created_start = datetime(year, month, day)
created_end = created_start + timedelta(days=1)
slug_value = 'my-slug-value'

posts = Post.all()
posts.filter('created_at >=', created_start)
posts.filter('created_at <', created_end)
posts.filter('slug =', slug_value)

# You can iterate over this query set just like a list
for post in posts:
    print post.key()

How about

from datetime import datetime, timedelta

created_start = datetime(year, month, day)
created_end = created_start + timedelta(days=1)
slug_value = 'my-slug-value'

posts = Post.all()
posts.filter('created_at >=', created_start)
posts.filter('created_at <', created_end)
posts.filter('slug =', slug_value)

# You can iterate over this query set just like a list
for post in posts:
    print post.key()
划一舟意中人 2024-08-13 02:19:00

您不需要“relativedelta” - 您所描述的是 datetime.timedelta。否则,你的答案看起来不错。

就处理时间而言,App Engine 的优点在于几乎所有查询都具有相同的每个结果成本 - 并且所有查询都与返回的记录成比例缩放,而不是与数据存储总大小成比例。因此,您的解决方案工作正常。

或者,如果您需要一个不等式过滤器来处理其他事情,您可以添加一个“created_day”DateProperty,并对其进行简单的相等检查。

You don't need 'relativedelta' - what you describe is a datetime.timedelta. Otherwise, your answer looks good.

As far as processing time goes, the nice thing about App Engine is that nearly all queries have the same cost-per-result - and all of them scale proportionally to the records returned, not the total datastore size. As such, your solution works fine.

Alternately, if you need your one inequality filter for something else, you could add a 'created_day' DateProperty, and do a simple equality check on that.

昔日梦未散 2024-08-13 02:19:00

最终使用relativedelta库+以jQuery风格链接过滤器,虽然还不太Pythonic,但编写起来更舒适,也更干燥。 :) 仍然不确定这是否是最好的方法,因为它可能需要更多的数据库处理时间?

date = datetime(int(year), int(month), int(day))
... # then
queryset = Post.objects_published()
                            .filter('created_at >=', date)
                            .filter('created_at <', date + relativedelta(days=+1))
                            ...

并将 slug 传递给 object_detail 视图或另一个过滤器。

Ended up using the relativedelta library + chaining the filters in jQuery style, which although not too Pythonic yet, is a tad more comfortable to write and much DRYer. :) Still not sure if it's the best way to do it, as it'll probably require more database processing time?

date = datetime(int(year), int(month), int(day))
... # then
queryset = Post.objects_published()
                            .filter('created_at >=', date)
                            .filter('created_at <', date + relativedelta(days=+1))
                            ...

and passing slug to the object_detail view or yet another filter.

薄荷港 2024-08-13 02:19:00

顺便说一句,您可以使用 datetime.timedelta。这可以让您找到日期范围或日期增量。

By the way you could use the datetime.timedelta. That lets you find date ranges or date deltas.

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