我有谷歌应用引擎活动模型。我需要在某个日期范围内查找事件。它可能是[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'>
答案 0 :(得分:3)
# 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)