ndb查询错误与datetime字段 - Google App Engine [英] ndb query error with datetime field - Google App Engine

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

问题描述



我在我的模型中定义了一个这样的字段。

  class Dates(ndb.model):
...
date = ndb.DateTimeProperty(required = True)#我想存储日期和时间
...

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

  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'对象没有属性'年'



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



我的数据库还是空的但我想这并不介意,因为我永远都不会有日期。



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

解决方案

您可以使用范围查询。参见下面的例子。

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

将获取所有的datetime与02/19/2013。 p>

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)

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

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.

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

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

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

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