带有日期时间字段的 ndb 查询错误 - Google App Engine [英] ndb query error with datetime field - Google App Engine

查看:27
本文介绍了带有日期时间字段的 ndb 查询错误 - Google App Engine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题,但找不到任何相关信息.

I'm having a problem and I don't find any information about.

我像这样在模型中定义了一个字段.

I define a field in my model like this.

class Dates(ndb.model):
    ...
    date = ndb.DateTimeProperty(required = True) # I want to store date and time
    ...

稍后我尝试查询(现在我想要一天的所有日期,我不介意时间):

Later I try a query (now I want all the dates for a day, I don'tn mind the time):

kl = Dates.query(ndb.AND(Dates.date.year == year,
                         Dates.date.month == month,
                         Dates.date.day == day),
                 ancestor = customer.key).fetch(keys_only = True)
dates = ndb.get_multi(kl)

但是我收到了这个错误日志:AttributeError: 'DateTimeProperty' 对象没有属性 'year'

But I get this error log: AttributeError: 'DateTimeProperty' object has no attribute 'year'

我不知道为什么.我试过 Dates.date() == date, Dates.date == date (<-DateTime obj), ...

I don't know why. I've tried Dates.date() == date, Dates.date == date (<-DateTime obj), ...

我的数据库仍然是空的,但我想这并不介意,因为我永远不会在每一个可能的日子里都有日期.

My DB is still empty but I suppose this doesn't mind because I'll never have dates for every possible days.

有人知道为什么吗?我应该改用 GQL 吗?

Anybody knows why? Should I go with GQL instead?

推荐答案

您可以为此使用范围"查询.请参阅下面的示例.

You can use "range" queries for this. See example below.

import datetime
date = datetime.datetime.strptime('02/19/2013', '%m/%d/%Y')
kl = Dates.query(
    ndb.AND(Dates.date >= date),
            Dates.date < date + datetime.timedelta(days=1))

将获取 02/19/2013 的所有日期时间.

Will fetch all datetime's with 02/19/2013.

这篇关于带有日期时间字段的 ndb 查询错误 - Google App Engine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆