我有谷歌应用程序引擎事件模型。我需要查找某个日期范围内的事件。可能是[2012年2月15日、2012年3月15日、2013年4月18日]。但当我搜索时,我遇到了异常。
模型类:
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)
这段代码无一异常(exception),但结果错误:
# 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 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)
关于python - 是否可以使用 'in' 关键字来过滤 ListProperty(item_type=datetime.date)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971111/