Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
web2py orderby datetime出现故障_Datetime_Sql Order By_Web2py - Fatal编程技术网

web2py orderby datetime出现故障

web2py orderby datetime出现故障,datetime,sql-order-by,web2py,Datetime,Sql Order By,Web2py,我正在web2py中编写一个简单的web应用程序,用于存储和检索数据库中的一些数据。为了使条目按时间顺序排列,我的表有一个存储日期和时间的属性。像这样: db.define_table('tablename', Field( .... .... .... Field('created_on','datetime', default=request.now, writable=False),

我正在web2py中编写一个简单的web应用程序,用于存储和检索数据库中的一些数据。为了使条目按时间顺序排列,我的表有一个存储日期和时间的属性。像这样:

db.define_table('tablename',
           Field( ....
           ....
           ....
           Field('created_on','datetime', default=request.now, writable=False),
           Field('last_modified','datetime', default=request.now, update=request.now),
           )
I'm passing them into a JSON dictionary object.Like so: 



d = { r.index_id : {'index_id':r.index_id,
                                 ....
                                 ....
                                 'comments':r.comments,
                                 'created_on':r.created_on,
                                 'last_modified':r.last_modified}
          for r in rows}

    return response.json(dict(entry_dict=d))
然后,当我请求数据时,我将orderby属性设置为last_modified:

rows = db().select(db.tablename.ALL, orderby=db.tablename.last_modified)
然后,我将结果传递到Json dictionary对象。像这样:

db.define_table('tablename',
           Field( ....
           ....
           ....
           Field('created_on','datetime', default=request.now, writable=False),
           Field('last_modified','datetime', default=request.now, update=request.now),
           )
I'm passing them into a JSON dictionary object.Like so: 



d = { r.index_id : {'index_id':r.index_id,
                                 ....
                                 ....
                                 'comments':r.comments,
                                 'created_on':r.created_on,
                                 'last_modified':r.last_modified}
          for r in rows}

    return response.json(dict(entry_dict=d))
当我从服务器得到响应时,我将dict返回给我的实际对象

MAIN.set('data_entries', data['entry_dict']);
然后,我在ractive中呈现结果:

{% #data_entries:index_id%}
              <tr class = "datarow" onclick="window.document.location='{% url+ '/'+ index_id %}';">
                  <td>{% index_id %}</td>
                  ....
                  <td>{% last_modified %}</td>
              </tr>
            {% /data_entries %}
{%#数据项:索引id%}
{%index_id%}
....
{%last_modified%}
{%/数据项%}
但是,当数据返回到webapp时,我会看到以下内容:


我这样做对吗

在Python中,字典不保留顺序,因此当您将
d
字典转换为JSON时,不一定会按照原始
对象的顺序获得键。您可以使用一个
OrderedDict
,但似乎您并不真的需要一个字典——只需创建一个列表(使用
as_list
方法):

然后在实践中,改变:

{% #data_entries:index_id %}
致:


在每个循环中似乎不需要索引值。相反,您将能够访问每个记录的
索引\u id
字段,作为节的数据上下文的一部分。

一旦您有
,您将如何生成显示?我刚刚用这些信息更新了我的帖子。