Fetching rows with st_asgeojson()

46 views
Skip to first unread message

Gael Princivalle

unread,
Sep 13, 2016, 9:36:32 AM9/13/16
to web2py-users
Hello.

Can someone explain me why when I use this set of rows:
events= db(db.events.id>0).select(db.events.id, db.events.title, db.events.geometry.st_asgeojson())
I can't call my rows with event.title but with event[db.events.title] ?

It's a problem because if I would like to call for a reference field like event[db.events.category.category_name] ticket is wrong attribute.

Thanks.

Gael Princivalle

unread,
Sep 17, 2016, 1:57:29 PM9/17/16
to web2py-users
Someone have an idea?

Here is the code that works:
    features= [{"type": "Feature",
                "properties": {
                    "popupContent": "<a href='" +  URL('default', 'event_page', vars=dict(event_id=event[db.events.id])) + "'>" + event[db.events.title] + "</a>",
                    "track_quotation": str(event[db.events.track_quotation])
                },
                "geometry": loads_json(event[db.events.geometry.st_asgeojson()])} for event in events]

But I would like to traduce it in a loop like that:
for event in events:
    ...

Massimo Di Pierro

unread,
Sep 18, 2016, 11:05:22 PM9/18/16
to web2py-users
How about this?

features = []
for event in events:
    link = A(event[db.events.title], _href=URL('default', 'event_page', vars=dict(event_id=event[db.events.id]))).xml()    
    item = {
        "type": "Feature",
        "popupContent": link,
        "track_quotation": str(event[db.events.track_quotation]),
        "geometry": loads_json(event[db.events.geometry.st_asgeojson()])
    }
    features.append(item)

Gael Princivalle

unread,
Oct 9, 2016, 3:55:33 PM10/9/16
to web2py-users
Thanks Massimo.

It works but the main problem still the same. I cannot call field properties (I don't know if it's the right term).

For example in the loop if I call for a datetime:
    for event in events:  
        item
= {
               
"type": "Feature",
                   
"properties": {
                       
"description": "Event date: " + str(event[db.events.event_datetime]),

                   
},
                   
"geometry": loads_json(event[db.events.geometry.st_asgeojson()])
       
}

Description is: Event date: 2017-01-10 09:00:00

If I would like to extract the day from the datetime, ticket is <type 'exceptions.KeyError'>:
    for event in events:  
        item
= {
               
"type": "Feature",
                   
"properties": {
                       
"description": "Event date: " + str(event[db.events.event_datetime.day]),

                   
},
                   
"geometry": loads_json(event[db.events.geometry.st_asgeojson()])
       
}

Perhaps it's due to the fact that I ask to the DAL a field value calling it by db.table.field, and db.events.event_datetime.day is not a field.

But how can I do it?

Calling event.event_datetime.day don't works anymore. Ticket is <type 'exceptions.AttributeError'>.

Thanks.
Reply all
Reply to author
Forward
0 new messages