web2py - show data from a MongoDB Document as a grid

61 views
Skip to first unread message

Nicola Barbieri

unread,
Jul 21, 2014, 9:51:59 AM7/21/14
to web...@googlegroups.com

Hi everyone,

I'm a web2py newbie and I'm having some hard time with the following simple task:

I would like to show in a page all the records stored in the following MongoDB document (called requests):

{
  "request": {
    "slice": [
      {
        "origin": "SFO",
        "destination": "MIA",
"date": "2014-08-21"
      }
    ]
  }
}

My approach would be define a table with a json field and then parse it:

db.py:

db.define_table('requests',
Field('request','json'))

default.py:

def all_records():
    grid = SQLFORM.grid(db.requests, fields=[db.requests.request.slice[0].origin,db.requests.request.slice[0].destination,db.requests.request.slice[0].date)
    return locals()


all_records.html:

{{extend 'layout.html'}}
{{=BEAUTIFY(response._vars)}}


Though, I get this error:

<type 'exceptions.AttributeError'> 'Field' object has no attribute 'slice'


so I guess I'm parsing the JSON file in the wrong way.


Any help is more than welcome,

thanks

Massimo Di Pierro

unread,
Jul 22, 2014, 2:37:48 AM7/22/14
to web...@googlegroups.com
web2py does not allow selecting only part of a JSON field. Sorry.

Nicola Barbieri

unread,
Jul 22, 2014, 11:47:28 AM7/22/14
to web...@googlegroups.com
Thanks for the prompt answer, Massimo.

Any suggestion on how I could approach my problem?
My goal would basically just query and show the records present in the MongoDB document shown in the post.


Thanks,

Nicola

Massimo Di Pierro

unread,
Jul 23, 2014, 5:22:14 AM7/23/14
to web...@googlegroups.com
I suggest you open a ticket about this and we could create an API to allow it. Yet this is very specific of mongodb and the grid is not really setup for this. 
Reply all
Reply to author
Forward
0 new messages