App Engine GQL:查询日期范围
此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
怎么样
How about
您不需要“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.
最终使用
relativedelta
库+以jQuery风格链接过滤器,虽然还不太Pythonic,但编写起来更舒适,也更干燥。 :) 仍然不确定这是否是最好的方法,因为它可能需要更多的数据库处理时间?并将 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?and passing slug to the
object_detail
view or yet another filter.顺便说一句,您可以使用 datetime.timedelta。这可以让您找到日期范围或日期增量。
By the way you could use the datetime.timedelta. That lets you find date ranges or date deltas.