從 datetime 提取年份操作

4 views
Skip to first unread message

SeedSeek

unread,
Nov 21, 2017, 8:22:21 PM11/21/17
to Uliweb
以下這樣會報錯,要如何修正或有其他替代方式?


class holiday(Model):
    ...
    dt_start = Field(datetime.datetime, verbose_name=_('時間'), index=True)
    ...
    
M_holiday = get_model('holiday')
    
for i in do_(select([distinct(func.extract('year', M_holiday.c['dt_start']) )] ) ).fetchall():
    print i



Traceback (most recent call last):
  File "apps\uliweb\contrib\staticfiles\wsgi_staticfiles.py", line 91, in __call__
    return self.app(environ, start_response)
  File "apps\uliweb\core\SimpleFrame.py", line 1485, in __call__
    response = self._open(environ)
  File "apps\uliweb\core\SimpleFrame.py", line 1430, in _open
    response = self.call_view(mod, handler_cls, handler, req, res, kwargs=values)
  File "apps\uliweb\core\SimpleFrame.py", line 973, in call_view
    result = self.call_handler(handler, request, response, env, wrap, args, kwargs)
  File "apps\uliweb\core\SimpleFrame.py", line 1084, in call_handler
    result = self._call_function(handler, request, response, env, args, kwargs)
  File "apps\uliweb\core\SimpleFrame.py", line 1077, in _call_function
    result = handler(*args, **kwargs)
  File "D:\Return\System\Downloads\PyLib\venv\my\my\apps\am\am_works_manager\views_WorkOff.py", line 372, in WorkOff_index
    print i
  File "apps/library\sqlalchemy\engine\result.py", line 153, in __repr__
    return repr(tuple(self))
  File "apps/library\sqlalchemy\engine\result.py", line 63, in __iter__
    yield processor(value)
  File "apps/library\sqlalchemy\processors.py", line 37, in process
    (type_.__name__, value))
ValueError: Couldn't parse datetime string '2016' - value is not a string.


Chunlin Zhang

unread,
Nov 21, 2017, 8:42:43 PM11/21/17
to uli...@googlegroups.com
是什么数据库?
能不能造个最小例子给limodou看看?

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.
To post to this group, send email to uli...@googlegroups.com.
Visit this group at https://groups.google.com/group/uliweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/uliweb/a1aedb50-8269-469d-8ec8-9ca9b8806bed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cnidance

unread,
Nov 22, 2017, 3:15:42 AM11/22/17
to uli...@googlegroups.com
已解決。

# postgresql
for i in do_(select([distinct(func.extract('year', self.L['db']['holiday'].c['dt_start']) )] ) ).fetchall():
    print i

# sqlite
for i in do_(select([distinct(func.strftime('%Y', self.L['db']['holiday'].c['dt_start']) )] ) ).fetchall():
    print i





~Ten ~

Reply all
Reply to author
Forward
0 new messages