关于flask-restful扩展中field.DataTime使用的求助

jtr109 发布于 2016/08/19 11:22
阅读 1K+
收藏 0
<p>我查阅document在 <strong>Flask Web开发</strong> 的基础上增加了<a href="http://flask-restful.readthedocs.io/en/latest/index.html">flask-restful扩展</a>来实现API。</p>


<p>相关代码如下,<a href="https://github.com/jtr109/ToDo/blob/dev/app/api_2_0/resources/todo_lists.py">源码请戳</a>:</p>


<pre><code>todo_list_fields = {
    'url': fields.String,
    'title': fields.String,
    'timestamp': fields.DateTime(dt_format='rfc822'),
    'master': fields.String,
    'tasks': fields.String,
    'events': fields.String,
}


def to_json_todo_list(todo_list):
    json_todo_list = {
        'url': url_for('api2.TodoListAPI', list_id=todo_list.id, _external=True),
        'title': todo_list.title,
        'timestamp': todo_list.timestamp,
        'master': url_for('api2.UserAPI', user_id=todo_list.master_id, _external=True),
        'tasks': url_for('api2.TasksAPI', list_id=todo_list.id, _external=True),
        'events': url_for('api2.EventsAPI', list_id=todo_list.id, _external=True),
    }
    return json_todo_list
</code></pre>


<p>但是在构建field.DateTime时发现,传入时间戳 (timestamp) 返回的JSON中,结果为"Thu, 18 Aug 2016 14:14:26 -0000",时间显示是正确的,但是最后的结尾为"-0000"而非"GMT":</p>


<pre><code>{
    "body": "task test",
    "state": "doing",
    "timestamp": "Thu, 18 Aug 2016 14:14:26 -0000",
    "todo_list": "http://jtr-todo.herokuapp.com/restful-api/v2.0/todo-lists/4", 
    "url": "http://jtr-todo.herokuapp.com/restful-api/v2.0/tasks/3"
}
</code></pre>


<p>于是我查看了<a href="http://flask-restful.readthedocs.io/en/latest/api.html#fields.DateTime">API文档</a>:</p>


<blockquote>
  <p>class fields.<strong>DateTime</strong>(dt_format='rfc822', **kwargs)</p>
  
  <p>Return a formatted datetime string in UTC. Supported formats are RFC 822 and ISO 8601.</p>
  
  <p>See <a href="https://docs.python.org/3/library/email.util.html#email.utils.formatdate">email.utils.formatdate()</a> for more info on the RFC 822 format.</p>
  
  <p>See <a href="https://docs.python.org/3/library/datetime.html#datetime.datetime.isoformat">datetime.datetime.isoformat()</a> for more info on the ISO 8601 format.</p>
  
  <p>Parameters:   dt_format (str) – 'rfc822' or 'iso8601'</p>
  
  <p><strong>format(value)</strong></p>
</blockquote>


<p>我使用的是'rfc822'即对应<a href="https://docs.python.org/3/library/email.util.html#email.utils.formatdate">email.utils.formatdate()</a>的文档,根据文档提示,我尝试将field写做<code>'timestamp': fields.DateTime(dt_format='rfc822', usegmt=True)</code>,结果显示</p>


<pre><code>...
 File "/Users/jtr109/mystuff/0to1/ToDo/app/api_2_0/resources/todo_lists.py", line 20, in &lt;module&gt;
</code></pre>


<p>       'timestamp': fields.DateTime(dt_format='rfc822', usegmt=True),
     File "/Users/jtr109/mystuff/0to1/ToDo/venv/lib/python2.7/site-packages/flask_restful/fields.py", line 354, in <strong>init</strong>
       super(DateTime, self).<strong>init</strong>(**kwargs)
    TypeError: <strong>init</strong>() got an unexpected keyword argument 'usegmt' </p>


<p>我该如何才能的到GMT呢?</p>
加载中
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部