我有谷歌应用程序引擎事件模型。我需要找到某个日期范围内的事件。可能是15.02.2012、15.03.2012、18.04.2013。但是当我搜索的时候,我得到了异常。
模型类:
class Event(db.Model):
title = db.StringProperty()
dates = db.ListProperty(item_type=datetime.date)
dates = [datetime.date.today(), datetime.date.today() + datetime.timedelta(days = 7), datetime.date.today() + datetime.timedelta(days = 14), datetime.date.today() + datetime.timedelta(days = 24)]下面是我的问题:
# exception
query = db.GqlQuery('SELECT * FROM Event WHERE dates in :dates', dates=dates)这段代码没有异常,但结果是错误的:
# 0 results, it's wrong
query = db.GqlQuery('SELECT * FROM Event WHERE dates in :dates', dates=[datetime.datetime.now()])有效,但我需要“in”:
query = db.GqlQuery('SELECT * FROM Event WHERE dates = DATE(2012, 1, 23)')同样的例外:
query = db.GqlQuery('SELECT * FROM Event WHERE dates in [DATE(2012, 1, 23)]')异常描述:
ERROR 2012-01-23 13:38:28,335 base.py:117] error: code='internal_server_error', message="Unsupported type for property : <type 'datetime.date'>"
ERROR 2012-01-23 13:38:28,345 base.py:119] Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "D:\project\eventinarea\eventinarea\handler\event.py", line 12, in get
tags=self.param('tags')
File "D:\project\eventinarea\eventinarea\logic\event.py", line 20, in search_events
logging.info(query.count())
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 2059, in count
raw_query = self._get_query()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\db\__init__.py", line 2633, in _get_query
self._cursor, self._end_cursor)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\gql\__init__.py", line 326, in Bind
query.update(enumerated_query)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1723, in update
self.__setitem__(filter, value)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore.py", line 1666, in __setitem__
datastore_types.ValidateProperty(' ', value, read_only=True)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\datastore_types.py", line 1480, in ValidateProperty
'Unsupported type for property %s: %s' % (name, v.__class__))
BadValueError: Unsupported type for property : <type 'datetime.date'>发布于 2012-01-24 09:16:04
# 0 results, it's wrong
query = db.GqlQuery('SELECT * FROM Event WHERE dates in :dates', dates=[datetime.datetime.now()]) 这不会产生结果的原因是您传入了now() (包括年/月/日和小时/分/秒/微秒),而实体的日期只是日期(年/月/日)。
下面这样的代码可能会起作用:
today = datetime.datetime.today()
today_date = datetime.datetime(year = today.year, month = today.month, day = today.day)
query = db.GqlQuery('SELECT * FROM Event WHERE dates in :dates', dates = [today_date]) 我认为你的原始查询失败的原因是:
BadValueError: Unsupported type for property : <type 'datetime.date'>查询需要datetime.datetime对象,但您传入的是datetime.date对象。
下面这样的代码可能会起作用:
today = datetime.datetime.today()
today_date = datetime.datetime(year = today.year, month = today.month, day = today.day)
dates = [today_date, today_date + datetime.timedelta(days = 7), today_date + datetime.timedelta(days = 14), today_date + datetime.timedelta(days = 24)]
query = db.GqlQuery('SELECT * FROM Event WHERE dates in :dates', dates = dates)https://stackoverflow.com/questions/8971111
复制相似问题