_extra is how web2py stores non-field values (e.g., the results of
expressions, such as aggregate functions) within Row objects. When you
access the item directly by subscripting the Row object, web2py
transparently retrieves it -- that is, tagsummary[0](count) will retrieve
the count from the first row. However, when converting to JSON, you get the
_extra dictionary included. I'm not sure there is an easy solution other
than to manipulate the Rows object before returning to the view. Something
like:
ts_list = tagsummary.as_list()
for row in ts_list:
[code to pull the count out of _extra and place it elsewhere]
Anthony
On Tuesday, November 8, 2011 3:09:43 AM UTC-5, BrendanC wrote:
>
> I'm trying to retrieve some summary data via an Ajax request with the
> following controller method.
> This is just a simple result set containing a list of tag names and usage
> counts retrieved in a JOIN query. Here's the code:
>
> def gettagsummary():
> '''
> Get list of tags and related counts (from tagref table) - user can
> then view by tag
> (similar to Stack Overflow tag view??)
> '''
>
> count = db.tagref.tag.count()
> tagsummary = db(db.tagref.tag==db.tag.id).select(db.tagref.tag,
> db.tag.name, count, \
> groupby=db.tagref.tag)
>
> return dict(tagsummary=tagsummary)
>
>
> From my webpage I make an Ajax/XHR request to get this info in json format
> (the request is url: "/search/gettagsummary.json"
>
> This is what the request response looks like:
>
> {"tagsummary": [
> {"tagref": {"tag": 1}, "tag": {"name": "Places"}, "_extra":
> {"COUNT(tagref.tag)": 4}},
>
> {"tagref": {"tag": 2}, "tag": {"name": "Cars"}, "_extra":
> {"COUNT(tagref.tag)": 2}},
> '
> .
> .
> .
> {"tagref": {"tag": 9}, "tag": {"name": "France"}, "_extra":
> {"COUNT(tagref.tag)": 2}},
> {"tagref": {"tag": 10}, "tag": {"name": "UK"}, "_extra":
> {"COUNT(tagref.tag)": 2}}
> ]}
>
> OK - so far, so good. Now I need to extract the count info in my Ajax
> response - However the count info is not easily (AFAIK) accessible - the
> '_extra' structure is a web2py artifact that looks odd. I'm thinking there
> must be a better way to pass the count info to my view (e.g. {"count":4}.
>
> So how do I change this so I can reference the count info. Do I need to
> change my controller code, or is there some (undocumented) way to reference
> the '_extra' info in the json response.
>
> I can't see anything in the docs on this - so hopefully someone here can
> point me in the right direction here.
>
> TIA,
> BrendanC
>